https://leetcode.com/problems/single-number/description/
题目:找出一个数组中只出现一次的那个数
思路:采用异或运算,比如数组[2,3,4,3,4,1,2],
2^3^4^3^4^1^2 = (2^2)^(3^3)^(4^4)^1 = 0^0^0^0^1 = 1
异或运算(相同为0,不同为1)可以采用交换律,
class Solution {
public int singleNumber(int[] nums) {
int result = 0;
for(int i = 0; i < nums.length;i++) {
result ^= nums[i];
}
return result;
}
}