1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() { println!("Hello, world!"); } struct Solution; use std::collections::HashMap; impl Solution { pub fn majority_element(nums: Vec<i32>) -> i32 { let mut table = HashMap::new(); for num in nums { *table.entry(num).or_insert(1) += 1; } *table.iter().reduce(|a,b| if a.1 > b.1 {a} else {b}).unwrap().0 } }