Degree of an Array
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
Find the smallest possible length of a contiguous subarray of nums that has the same degree as nums. Return that length.
Example 1
Input
nums = [1,2,2,3,1]Output
2The array has degree 2 because both 1 and 2 appear twice, and the shortest subarray with degree 2 is [2, 2] with length 2.
Example 2
Input
nums = [1,2,2,3,1,4,2]Output
6The degree is 3 because 2 appears three times, and the shortest subarray with degree 3 is [2,2,3,1,4,2] with length 6.
Constraints
- nums.length will be between 1 and 50,000.
- nums[i] will be an integer between 0 and 49,999.