1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/rs/count-elements-with-maximum-frequency.rs

14 lines
376 B
Rust
Raw Normal View History

use std::collections::HashMap;
impl Solution {
pub fn max_frequency_elements(nums: Vec<i32>) -> i32 {
let mut freqs: HashMap<i32, usize> = HashMap::new();
for &x in &nums {
*freqs.entry(x).or_insert(0) += 1;
}
let m = *freqs.values().max().unwrap();
(m * freqs.iter().filter(|&(_, f)| *f == m).count()) as i32
}
}