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
Inputnums = [1,2,2,3,1]
Output2
The 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
Inputnums = [1,2,2,3,1,4,2]
Output6
The 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.

Asked at 11 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate