This function computes the mode of a Vec<i32>
using a HashMap
to keep count of the occurrence of each value. I do not understand why this will not compile unless the key is deferenced twice in this last line:
fn mode(vec: &Vec<i32>) -> i32 {
let mut counts = HashMap::new();
for n in vec {
let count = counts.entry(n).or_insert(0);
*count += 1;
}
**counts.iter().max_by_key(|a| a.1).unwrap().0
}