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.
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 will be between 1 and 50,000.
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