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?
思路:
利用异或的特性:
1)a^b = b^a
2)a^b^c = (a^b)^c = a^(b^c)
3)0^n = n(任何数与0异或都等于它本身)。
Solution:
public class Solution {
public int singleNumber(int[] A) {
int result = 0;
for(int i = 0;i<A.length;i++){
result = result^A[i];
}
return result;
}
}