Given an array of integers, every element appears twice except for one. Find that single one.
public class Solution {
/*
* 将数组里的值逐个进行异或运算
* 最终类似有0000 ^ 1010 = 1010
* 1010即那个唯一的数
*/
public int singleNumber(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int result = nums[0];
for(int i = 1; i < nums.length; i++){
result = result ^ nums[i];
}
return result;
}
}