Back to course home
0% completed
Solution: Degree of an Array
Problem Statement
You are given an array nums
containing positive integers
. The degree
of nums
is defined as the maximum frequency
of any one of its elements
.
Return the minimum length
of a contiguous subarray
of nums
, that has the same degree
as nums.
Example
-
Example 1:
- Input: nums =
[1, 4, 2, 3, 2]
- Expected Output:
3
- Justification: The array's degree is 2 as it appears twice. The shortest subarray with a degree of 2 is
[2, 3, 2]
, which has a length of 3.
- Input: nums =
-
Example 2:
- Input: nums =
[4, 9, 4, 4, 5, 5, 4]
- Input: nums =
.....
.....
.....
Like the course? Get enrolled and start learning!