#3005
Easy Algorithms Count elements with maximum frequency
Array Hash Table Counting
79.8% acceptance
Feb 25, 2026
1083
96
You are given an array nums consisting of positive integers.
Return the total frequencies of elements in nums such that those elements all have the maximum frequency.
The frequency of an element is the number of occurrences of that element in the array.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn max_frequency_elements(nums: Vec<i32>) -> i32 {
let mut freq = std::collections::HashMap::new();
for n in &nums { *freq.entry(n).or_insert(0) += 1; }
let max_f = *freq.values().max().unwrap();
freq.values().filter(|&&v| v == max_f).map(|&v| v).sum()
}
}