题目:
Given an array of integers, every element appears twice except for one. Find that single one.
解法1,hash map 如代码中注释掉的部分;
解法2,bit manipulation 异或法
class Solution {
public:
int singleNumber(vector<int>& nums) {
/*unordered_map<int, int> m;
for(int num : nums) m[num]++;
for(auto it : m) if(it.second == 1) return it.first;
return 0;*/
int ans = 0;
for(int i = 0; i < nums.size(); i++)
ans ^= nums[i];
return ans;
}
};