mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
rs: add «3005. Count Elements With Maximum Frequency»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
f65d94b769
commit
fe9f5ac9da
1 changed files with 13 additions and 0 deletions
13
rs/count-elements-with-maximum-frequency.rs
Normal file
13
rs/count-elements-with-maximum-frequency.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue