Degree of an Array (LeetCode 697): Solution and Explanation

Easy
Array
Hash Table

The answer is the shortest span from the first to the last copy of a most frequent value. One scan with two hash maps (first index, count) finds it.

Intuition

The degree is the count of the most frequent value. A subarray only has that same degree if it contains every copy of one of those most frequent values.

For a fixed value x, the smallest subarray containing all copies of x starts at its first copy and ends at its last copy. Anything shorter loses a copy, anything longer is wasted.

So the task reduces to: among values with the highest count, find the one whose first and last copies are closest. While scanning, index i is the latest copy of nums[i], so i - first[x] + 1 is its span so far.

Approach: One pass with first index and count

  1. Keep a map first from value to the index where it first appeared, and a map count from value to how many times it has appeared.
  2. Scan i from left to right with x = nums[i]. Record first[x] if missing and add 1 to count[x].
  3. Compute span = i - first[x] + 1.
  4. If count[x] > degree, or count[x] == degree and span < best, set degree = count[x] and best = span.
  5. Return best.

Step-by-step example

nums = [1,2,2,3,1]
  1. i = 0, x = 1: first[1] = 0, count 1, span 1. It beats degree 0, so degree = 1 and best = 1.
  2. i = 1, x = 2: first[2] = 1, count 1, span 1. It ties degree 1 but is not shorter, so nothing changes.
  3. i = 2, x = 2: count[2] = 2, span from 1 to 2 is 2. It beats degree 1, so degree = 2 and best = 2.
  4. i = 3, x = 3: count 1, below the degree.
  5. i = 4, x = 1: count[1] = 2, span from 0 to 4 is 5. It ties degree 2 but 5 is not shorter than 2.
  6. The answer is 2, the window [2,2].

Code: Python, Java, C++

1class Solution:
2    def findShortestSubArray(self, nums: List[int]) -> int:
3        first = {}
4        count = {}
5        degree = best = 0
6        for i, x in enumerate(nums):
7            first.setdefault(x, i)
8            count[x] = count.get(x, 0) + 1
9            span = i - first[x] + 1
10            if count[x] > degree or (count[x] == degree and span < best):
11                degree = count[x]
12                best = span
13        return best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each element is handled once with O(1) hash map operations.

Space

O(n)

The two maps hold one entry per distinct value.

Other approaches

Three maps, two passes (time O(n), space O(n))

First record first index, last index and count for every value, then loop over the values with the maximum count and take the smallest last - first + 1. Same complexity, slightly easier to explain.

Check every subarray (time O(n^2), space O(n))

Compute the degree of every subarray and keep the shortest that matches. Far too slow for 50,000 elements.

Common mistakes

  • Returning the degree instead of the subarray length.
  • Picking the first most frequent value found. When several values share the degree, the one with the shortest span wins.
  • Forgetting that a single element is a valid answer: if every value is distinct, the degree is 1 and the answer is 1.

Frequently asked questions

Why does the window have to start at the first copy and end at the last copy?

To keep the same degree, the subarray must hold all copies of some most frequent value. The tightest window doing that is from its first copy to its last copy.

Why can a single pass compare spans before the scan ends?

At index i, count[x] and the span i - first[x] + 1 describe x up to now. When x reaches its final count, that span is its full span, and the update rule compares it fairly with the other values that reached the same count.

Could I use arrays instead of hash maps?

Yes. Values are below 50,000, so two integer arrays of that size work and avoid hashing. Hash maps keep the code general.