# Topic

## Example_1

[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]

## Tips

nums.length 在1到 50,000 区间范围内。
nums[i] 是一个在 0 到 49,999 范围内的整数。

# Solution_1

## Code_1

class Solution:
def findShortestSubArray(self, nums: List[int]) -> int:
d = collections.Counter(nums)
degree = max(d.values())
if degree == 1:
return 1
res = len(nums)

for i, j in d.items():
if j == degree:
start = 0
end = len(nums) - 1

while nums[start] != i:
start += 1

while nums[end] != i:
end -=1
res = min(res, end - start + 1)
return res



# Solution_2

## Code_2

class Solution:
def findShortestSubArray(self, nums: List[int]) -> int:
mp = {}

for i, num in enumerate(nums):
if num in mp:
mp[num][0] += 1
mp[num][2] = i
else:
mp[num] = [1, i, i]

degree = res = 0
for count, start, end in mp.values():
if degree < count:
degree = count
res = end - start + 1
elif degree == count:
if res > end - start + 1:
res = end - start + 1

return res


10-31 808
10-31 545
09-23 473
05-03 158
04-13 652
10-14 182