All problems

Degree of an Array

Easy · hash map

The degree of a list of integers is the highest number of times any single value appears in it. For example, [5, 3, 5, 8, 3, 5] has degree 3, because 5 appears three times and nothing appears more often.

Given a list nums, find a contiguous stretch of it (a *subarray*, at least one element long) that has the same degree as the whole list, and return the length of the shortest such stretch.

Examples

Example 1

Input: nums = [1, 2, 2, 3, 1]

Output: 2

The degree is 2 (both 1 and 2 appear twice). The stretch [2, 2] also has degree 2 and has length 2.

Example 2

Input: nums = [1, 2, 2, 3, 1, 4, 2]

Output: 6

The degree is 3, because 2 appears three times. The shortest stretch that holds all three 2s is [2, 2, 3, 1, 4, 2].

Example 3

Input: nums = [7]

Output: 1

Constraints