From fe9f5ac9da0911fad6c909f67e9083396da21b13 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 8 Mar 2024 19:59:26 +0100 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB3005.=20Count=20Elements=20Wi?= =?UTF-8?q?th=20Maximum=20Frequency=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/count-elements-with-maximum-frequency.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 rs/count-elements-with-maximum-frequency.rs 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 + } +}