https://leetcode.com/problems/single-number/
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^a = 0;
2、0^a = a;
所以,如果对所有元素做异或运算,其结果为那个出现一次的元素,得解。
int singleNumber(int* nums, int numsSize) {
int i;
int ans = nums[0];
for(i = 1; i < numsSize; i++) {
ans ^= nums[i];
}
return ans;
}