Count Elements With Maximum Frequency — Problem & Solution
Count Elements With Maximum Frequency solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Count Elements With Maximum Frequency is an easy coding problem on arrays, hash table and counting, asked in Amazon, TCS and CRED interviews.
- Difficulty: Easy
- Topics: Arrays, Hash Table, Counting
- Asked at: Amazon, TCS, CRED
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an array of positive integers nums, return the total number of elements that have the maximum frequency.
If several values tie for the highest frequency, every one of their occurrences is counted.
Example 1
Input: nums = [1,2,2,3,1,4]
Output: 4
Explanation: 1 and 2 both occur twice, contributing 2 + 2 = 4 elements.
Example 2
Input: nums = [1,2,3,4,5]
Output: 5
Explanation: Every value occurs once, so all five count.
Example 3
Input: nums = [7,7,7]
Output: 3
Constraints
1 <= nums.length <= 1001 <= nums[i] <= 100
Hints for Count Elements With Maximum Frequency
Hint 1
Count the frequencies, then find the maximum.
Hint 2
Sum that maximum once for every value that attains it.
Hint 3
Equivalently:
maxFreq × (number of values with maxFreq).
How to solve Count Elements With Maximum Frequency
Two sweeps over the frequency table — one to find the peak, one to add up every value that reaches it.
Approach
- Tally the occurrences of each value.
- Find the largest tally.
- Add that tally once per value achieving it.
Why it works
The question counts elements, not distinct values, so each tying value contributes its whole frequency rather than 1.
Complexity
- Time —
O(n) - Space —
O(V)
Pitfalls
- Counting the number of tying values instead of their total elements answers 2 rather than 4 on the first example.
- Finding the max and summing in one pass is possible but easy to get wrong when the max changes mid-sweep.
Reference solution
Python
from typing import List
def maxFrequencyElements(nums: List[int]) -> int:
count = {}
for x in nums:
count[x] = count.get(x, 0) + 1
best = max(count.values())
return sum(c for c in count.values() if c == best)Java
public static int maxFrequencyElements(int[] nums) {
int[] count = new int[101];
for (int x : nums) count[x]++;
int best = 0;
for (int v = 1; v <= 100; v++) if (count[v] > best) best = count[v];
int total = 0;
for (int v = 1; v <= 100; v++) if (count[v] == best) total += best;
return total;
}C++
int maxFrequencyElements(vector<int>& nums) {
vector<int> count(101, 0);
for (int x : nums) count[x]++;
int best = 0;
for (int v = 1; v <= 100; v++) if (count[v] > best) best = count[v];
int total = 0;
for (int v = 1; v <= 100; v++) if (count[v] == best) total += best;
return total;
}JavaScript
var maxFrequencyElements = function(nums) {
const count = new Array(101).fill(0);
for (let i = 0; i < nums.length; i++) count[nums[i]]++;
let best = 0;
for (let v = 1; v <= 100; v++) if (count[v] > best) best = count[v];
let total = 0;
for (let v = 1; v <= 100; v++) if (count[v] === best) total += best;
return total;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Hashing: Hash Maps and Hash Sets