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. Your task is to find the smallest possible length of a (contiguous) subarray of nums that has the same degree as nums.

30:00

Degree of an Array
easy
Free written solution
Topics
Companies

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. Your task is to find the smallest possible length of a (contiguous) subarray of nums that has the same degree as nums.

Example 1:
Input: {"nums":[1,2,2,3,1]}
Output: 2
Constraints:
  • nums.length\text{nums.length} will be between 1 and 50,000.

  • nums[i]\text{nums}[i] will be an integer between 0 and 49,999.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,2,3,1]}

Two maps: where each value first appeared, and how often it has appeared. degree and best start at 0.

nums: blue bar = span of x from its first copy to i, green bar = best window

1

0

2

1

2

2

3

3

1

4

value -> first index, count

degree (max count)

0

best length

0

Variables
VariableValue
i-
x-
degree0
best0
DepthFunction Call
Stack empty
0/12