Count Elements With Maximum Frequency
EasyUpdated: Aug 2, 2025
Practice on:
Problem
You are given an array nums consisting of positive integers.
Return thetotal frequencies of elements in __nums such that those elements all have themaximum frequency.
The frequency of an element is the number of occurrences of that element in the array.
Examples
Example 1
Input: nums = [1,2,2,3,1,4]
Output: 4
Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.
Example 2
Input: nums = [1,2,3,4,5]
Output: 5
Explanation: All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.
Constraints
1 <= nums.length <= 1001 <= nums[i] <= 100
Solution
Method 1 – Hash Map Frequency Counting
Intuition
We count the frequency of each element, find the maximum frequency, and sum the counts of all elements that have this maximum frequency.
Approach
- Use a hash map to count the frequency of each element in the array.
- Find the maximum frequency among all elements.
- Sum the frequencies of all elements that have this maximum frequency.
- Return the sum.
Code
C++
class Solution {
public:
int maxFrequencyElements(vector<int>& nums) {
unordered_map<int, int> cnt;
int mx = 0, ans = 0;
for (int x : nums) cnt[x]++;
for (auto& [k, v] : cnt) mx = max(mx, v);
for (auto& [k, v] : cnt) if (v == mx) ans += v;
return ans;
}
};
Go
func maxFrequencyElements(nums []int) int {
cnt := map[int]int{}
mx, ans := 0, 0
for _, x := range nums { cnt[x]++ }
for _, v := range cnt { if v > mx { mx = v } }
for _, v := range cnt { if v == mx { ans += v } }
return ans
}
Java
class Solution {
public int maxFrequencyElements(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
int mx = 0, ans = 0;
for (int x : nums) cnt.put(x, cnt.getOrDefault(x, 0) + 1);
for (int v : cnt.values()) mx = Math.max(mx, v);
for (int v : cnt.values()) if (v == mx) ans += v;
return ans;
}
}
Kotlin
class Solution {
fun maxFrequencyElements(nums: IntArray): Int {
val cnt = mutableMapOf<Int, Int>()
var mx = 0
var ans = 0
for (x in nums) cnt[x] = cnt.getOrDefault(x, 0) + 1
for (v in cnt.values) mx = maxOf(mx, v)
for (v in cnt.values) if (v == mx) ans += v
return ans
}
}
Python
class Solution:
def maxFrequencyElements(self, nums: list[int]) -> int:
from collections import Counter
cnt = Counter(nums)
mx = max(cnt.values())
return sum(v for v in cnt.values() if v == mx)
Rust
use std::collections::HashMap;
impl Solution {
pub fn max_frequency_elements(nums: Vec<i32>) -> i32 {
let mut cnt = HashMap::new();
for &x in &nums { *cnt.entry(x).or_insert(0) += 1; }
let mx = *cnt.values().max().unwrap();
cnt.values().filter(|&&v| v == mx).map(|&v| v).sum()
}
}
TypeScript
class Solution {
maxFrequencyElements(nums: number[]): number {
const cnt: Record<number, number> = {};
for (const x of nums) cnt[x] = (cnt[x] || 0) + 1;
const mx = Math.max(...Object.values(cnt));
return Object.values(cnt).filter(v => v === mx).reduce((a, b) => a + b, 0);
}
}
Complexity
- ⏰ Time complexity:
O(n), where n is the length of the array, for counting and summing. - 🧺 Space complexity:
O(n), for storing the frequency map.