feat: 3005_count_elements_with_maximum_frequency
This commit is contained in:
parent
ea8fd35c8a
commit
c225277c72
7
3005_count_elements_with_maximum_frequency/Cargo.lock
generated
Normal file
7
3005_count_elements_with_maximum_frequency/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "count_elements_with_maximum_frequency"
|
||||
version = "0.1.0"
|
6
3005_count_elements_with_maximum_frequency/Cargo.toml
Normal file
6
3005_count_elements_with_maximum_frequency/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "count_elements_with_maximum_frequency"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
24
3005_count_elements_with_maximum_frequency/src/lib.rs
Normal file
24
3005_count_elements_with_maximum_frequency/src/lib.rs
Normal file
@ -0,0 +1,24 @@
|
||||
pub struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn max_frequency_elements(nums: Vec<i32>) -> i32 {
|
||||
let mut map = std::collections::HashMap::<i32, i32>::new();
|
||||
|
||||
for num in nums {
|
||||
*map.entry(num).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
let mut max_freq = 0;
|
||||
let mut max_freq_count = 0;
|
||||
for freq in map.values() {
|
||||
if *freq > max_freq {
|
||||
max_freq = *freq;
|
||||
max_freq_count = 1;
|
||||
} else if *freq == max_freq {
|
||||
max_freq_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
max_freq_count * max_freq
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user