题目

题解一:采用map集合

class Solution {
   public static int singleNumber(int[] nums) {
        Map map = new HashMap<Integer,Integer>();
        for (int i = 0; i < nums.length; i++) {
        //判断key是否重复,重复直接删掉重复的key
            if (map.containsKey(nums[i])) {
                map.remove(nums[i]);
            }else {
            //不重复直接添加
                map.put(nums[i], i);
            }
        }
        //根据题意其实最后只会剩一个  不用循环也行
        int convertedKey = 0;
        for (Object key : map.keySet()) {
            convertedKey = (int) key;
        }
        return convertedKey;
    }
}
题解二:异或(骚气解题)
- a^0 = a;
- a^a = 0;
- a^b ^b =a;
-  
  也就是说,只要里面只有一个重复的数,那么和其他存在重复的数进行异或最终只会得到自己 

   /**
     * 异或运算符
     *
     * @param nums
     * @return
     */
    public static int singleNumber(int[] nums) {
        int single = 0;
        for (int num : nums) {
            single = single^num;
        }
     return single;
    }



















