Count Elements With Maximum Frequency (LeetCode 3005): Solution and Explanation
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
- Keep a count per value (a hash map, or an array of size 101 because values are at most 100), plus
best = 0andtotal = 0. - For each element
x, increase its count toc. - If
c > best, setbest = candtotal = c. - Otherwise, if
c == best, addctototal. - Return
total.
Step-by-step example
nums = [1,2,2,3,1,4]
- 1 gets count 1, which beats best 0, so best = 1 and total = 1.
- 2 gets count 1, which ties best 1, so total = 2.
- The second 2 gets count 2, which beats best 1, so best = 2 and total = 2.
- 3 gets count 1, below best 2, so nothing changes.
- The second 1 gets count 2, which ties best 2, so total = 4.
- 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 total1class Solution {
2 public int maxFrequencyElements(int[] nums) {
3 int[] count = new int[101];
4 int best = 0, total = 0;
5 for (int x : nums) {
6 int c = ++count[x];
7 if (c > best) {
8 best = c;
9 total = c;
10 } else if (c == best) {
11 total += c;
12 }
13 }
14 return total;
15 }
16}1class Solution {
2public:
3 int maxFrequencyElements(vector<int>& nums) {
4 vector<int> count(101, 0);
5 int best = 0, total = 0;
6 for (int x : nums) {
7 int c = ++count[x];
8 if (c > best) {
9 best = c;
10 total = c;
11 } else if (c == best) {
12 total += c;
13 }
14 }
15 return total;
16 }
17};Time and space complexity
O(n)
Each element is counted once and the comparison with best is constant time.
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.