mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 01:15:57 +08:00
25 lines
549 B
Go
25 lines
549 B
Go
package leetcode
|
|
|
|
func findShortestSubArray(nums []int) int {
|
|
frequency, maxFreq, smallest := map[int][]int{}, 0, len(nums)
|
|
for i, num := range nums {
|
|
if _, found := frequency[num]; !found {
|
|
frequency[num] = []int{1, i, i}
|
|
} else {
|
|
frequency[num][0]++
|
|
frequency[num][2] = i
|
|
}
|
|
if maxFreq < frequency[num][0] {
|
|
maxFreq = frequency[num][0]
|
|
}
|
|
}
|
|
for _, indices := range frequency {
|
|
if indices[0] == maxFreq {
|
|
if smallest > indices[2]-indices[1]+1 {
|
|
smallest = indices[2] - indices[1] + 1
|
|
}
|
|
}
|
|
}
|
|
return smallest
|
|
}
|