Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Degree of an Array

XPChallenge Points: 10
levelLevel: Easy

You are given an array nums containing non-negative integers.
The degree of the array refers to the highest number of times any value appears in the entire array.

Your goal is to determine the minimum length of a contiguous subarray whose degree is equal to the degree of the original array.
In other words, the subarray must include all occurrences of whichever number(s) appear the most.

Example 1:

Input: nums = [4, 5, 4, 6, 5]

Output: 3

Explanation:

Both 4 and 5 appear twice, so the array degree is 2. Subarrays with degree 2 include: [4,5,4], [5,4,6,5], [5,4,6]… The smallest valid one is [4,5,4] of length 3.

Example 2:

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

Output: 5

Explanation:

2 appears three times, which is the maximum. All three occurrences of 2 span the entire array, so the smallest valid subarray length is 5.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result