Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- fn main() {
- // (Key, value)
- let list = vec![(1, 2), (3, 4), (1, 1), (3, 6), (2, 8), (1, 3)];
- // Look for max value for each key in list
- let mut max = HashMap::new();
- for &(n, x) in &list {
- let mut cur_max = max.entry(n).or_insert(x);
- if x > *cur_max {
- *cur_max = x;
- }
- }
- println!("{:?}", max);
- }
Add Comment
Please, Sign In to add comment