12345678910111213141516171819202122 |
- use std::collections::HashMap;
-
- // I'm fairly sure there's a better more mathsy way of doing this but I can't work it out yet.
- pub fn single_number(nums: Vec<i32>) -> i32 {
- let mut hm = HashMap::<i32, i32>::new();
- for n in nums {
- if hm.contains_key(&n) {
- hm.remove(&n);
- } else {
- hm.insert(n, 0);
- }
- }
- match hm.keys().next() {
- Some(i) => *i,
- _ => 0
- }
- }
-
- pub fn main() {
- println!("Expected: 1");
- println!("Got: {}", single_number(vec![1, 2, 3, 4, 3, 4, 2]));
- }
|