Count Elements With Maximum Frequency (LeetCode 3005): Solution and Explanation

Easy
Array
Hash Table
Counting

Find the highest number of times any value appears in the array, then return how many elements belong to values that appear exactly that many times.

Intuition

Picture one column per distinct value, and drop each element onto its column as you read the array. The tallest height is the maximum frequency, and the answer is the number of blocks sitting in columns of that height.

You could count everything first and then look for the tallest columns. But each new block only raises one column by one, so the effect on the answer is easy to track as you go.

If the column that just grew is now taller than the old best, it is the only column at the new height, so total becomes its count. If it exactly ties the best, its blocks join the total. If it is still shorter, nothing changes.

Approach: One pass with a running best

  1. Keep a count per value (a hash map, or an array of size 101 because values are at most 100), plus best = 0 and total = 0.
  2. For each element x, increase its count to c.
  3. If c > best, set best = c and total = c.
  4. Otherwise, if c == best, add c to total.
  5. Return total.

Step-by-step example

nums = [1,2,2,3,1,4]
  1. 1 gets count 1, which beats best 0, so best = 1 and total = 1.
  2. 2 gets count 1, which ties best 1, so total = 2.
  3. The second 2 gets count 2, which beats best 1, so best = 2 and total = 2.
  4. 3 gets count 1, below best 2, so nothing changes.
  5. The second 1 gets count 2, which ties best 2, so total = 4.
  6. 4 gets count 1, below best. The answer is 4.

Code: Python, Java, C++

1class Solution:
2    def maxFrequencyElements(self, nums: List[int]) -> int:
3        count = {}
4        best = total = 0
5        for x in nums:
6            count[x] = count.get(x, 0) + 1
7            c = count[x]
8            if c > best:
9                best, total = c, c
10            elif c == best:
11                total += c
12        return total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
One pass with a running best O(n)
Count, then scan O(n)
Sort and group O(n log n)
Time

O(n)

Each element is counted once and the comparison with best is constant time.

Space

O(k)

One counter per distinct value, at most 100 here.

Other approaches

Count, then scan (time O(n), space O(k))

Build the full frequency map, take the maximum frequency, then add up the frequencies equal to it. Two simple passes and just as fast.

Sort and group (time O(n log n), space O(1))

Sort the array so equal values sit together, measure each run length, and track the longest runs. Works without a hash map but sorting costs more.

Common mistakes

  • Returning the number of values with the maximum frequency instead of the number of elements. For [1,2,2,3,1,4] that would give 2 instead of 4.
  • Returning the maximum frequency itself.
  • In the one-pass version, adding to total when a count beats best instead of resetting it. The old leaders are no longer at the top.

Frequently asked questions

Why does total reset to c when a new best appears?

The value that just grew is the only one at the new height, because every other column is at most the old best. So it alone makes up the total.

Is the answer always a multiple of the maximum frequency?

Yes. It equals the maximum frequency times the number of values that reach it.

What if every value is distinct?

Every value appears once, so all of them tie at frequency 1 and the answer is the length of the array.