Facebook
From Bitty Hog, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 174
  1. class Solution {
  2.     public int singleNumber(int[] nums) {
  3.         HashMap<Integer,Integer> map = new HashMap<>();
  4.         for(int i = 0 ; i < nums.length ; ++i){
  5.             if(map.containsKey(nums[i])){
  6.                 map.put(nums[i], map.get(nums[i]) +1);
  7.             } else {
  8.                 map.put(nums[i],1);
  9.             }
  10.         }
  11.         for(Map.Entry<Integer,Integer> map2 : map.entrySet()){
  12.             if(map2.getValue() == 1){
  13.                 return map2.getKey();
  14.             }
  15.         }
  16.         throw new IllegalArgumentException();
  17.     }
  18. }
  19. class Solution {
  20.         public int singleNumber(int[] nums) {
  21.         int x = 0;
  22.         for(int i: nums){
  23.             x ^= i;
  24.         }
  25.         return x;
  26.     }
  27. }
  28.