题目描述
Given an array of integers, every element appears twiceexcept for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
分析: 1.异或满足交换律。
2.相同两个数异或为0。
3.0异或一个数为那个数本身。
class Solution {
public:
int singleNumber(int A[], int n) {
int x = 0;
for(int i = 0; i < n; i++)
{
x ^= A[i];
}
return x;
}
};