My LeetCode grinding. Trying to do a problem a day.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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