136. 只出现一次的数字(简单)

简单题,但是。。。他喵的。

以下数学性质:

  1. 交换律:a ^ b ^ c <=> a ^ c ^ b

  2. 任何数于0异或为任何数 0 ^ n => n

  3. 相同的数异或为0: n ^ n => 0

var a = [2,3,2,4,4]

2 ^ 3 ^ 2 ^ 4 ^ 4等价于 2 ^ 2 ^ 4 ^ 4 ^ 3 => 0 ^ 0 ^3 => 3

class Solution {
    public int singleNumber(int[] nums) {
        int res = 0;
        for(int i =0; i<nums.length;i++){
            res ^= nums[i];
        }
        return res;
    }
}