Given an array of integers, every element appears twice except for one. Find the single one.
Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?
Given a series of numbers, only one of them appears once, and the remaining numbers appear two times. For example:
1 2 3 2 4 3 1
The single number in the sequence is 4.
Note: A single number is found in the linear time and no additional memory space can be used.
Depending on the bit manipulation in the hint, you can think of:
- Any one number with 0 XOR, the resulting result is its own.
- Two equal number XOR, with a result of 0.
Therefore, the number of the given array is different or, in turn, the number of the occurrence of the different or after the 0, the final result is the desired single.
int singleNumber(vector<int>& nums) { int0; for (int0; i < nums.size(); i++){ ans = ans^nums[i]; } return ans; }
[Leetcode] Single number