diff --git a/rs/count-elements-with-maximum-frequency.rs b/rs/count-elements-with-maximum-frequency.rs new file mode 100644 index 0000000..a9c1d18 --- /dev/null +++ b/rs/count-elements-with-maximum-frequency.rs @@ -0,0 +1,13 @@ +use std::collections::HashMap; + +impl Solution { + pub fn max_frequency_elements(nums: Vec) -> i32 { + let mut freqs: HashMap = 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 + } +}