According to bitwise operator XOR:

  • x ^ x = 0
  • y ^ 0 = y

We apply the XOR operator to all the nums, all the same numbers will apply x ^ x = 0, and then y ^ 0 = y will result the single number.

class Solution {
public:
	int singleNumber(vector<int>& nums) {
		int xorN = 0;
		for (auto iter = nums.begin(); iter != nums.end(); ++iter) {
			xorN ^= *iter;
		}
		return xorN;
	}
};