※ 引述《sustainer123 (caster )》之銘言:
: 3005. Count Elements With Maximum Frequency
: 計算出現最多次的元素之次數之總和
: 思路:
: 用哈希表計算次數 最後比大小加總
開始習慣用Rust的小雞巴東西跟縫合怪了==
Code:
use std::collections::HashMap;
impl Solution {
pub fn max_frequency_elements(nums: Vec<i32>) -> i32 {
let mut counts = HashMap::new();
for num in nums {
*counts.entry(num).or_insert(0) += 1;
}
let max_frequency = counts.values().max().unwrap();
counts.iter().filter(|(_, &v)| v == *max_frequency)
.map(|(_, &v)| v).sum()
}
}