Single Number III
Implement singleNumber
This time the array hides not one lone value but two — every other number is matched with a twin, while these two odd ones out have no partner anywhere in the array. Track both down and hand them back, in whichever order is convenient.
A single XOR pass over the whole array cancels every paired value, but with two different singles left over instead of one, the result is
a ^ b rather than a clean answer on its own. The fix is to find one bit where a and b must disagree (any bit set in a ^ b works), and use it to split every number into two groups — XOR-ing each group separately then isolates one single per group.
Example 1:
Input: nums = [1,2,1,3,2,5]
Output: [3,5]
Example 2:
Input: nums = [-1,0]
Output: [-1,0]
Example 3:
Input: nums = [0,1]
Output: [0,1]
+ 10 hidden test cases run on Submit.
Constraints:
- ●
2 ≤ nums.length ≤ 3 × 10⁴ - ●
-3 × 10⁴ ≤ nums[i] ≤ 3 × 10⁴ - ●
Exactly two values in nums appear exactly once; every other value appears exactly twice
nums =
[1, 2, 1, 3, 2, 5]