Question:
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
两个相同的数字异或的结果是0.
代码如下:
public int singleNumber(int[] A) {
int single = 0;
int length = A.length;
if (length == 0) {
return -1;
}
for (int i = 0; i < length; i++) {
single ^= A[i];
}
return single;
}