diff = lowest set bit of (a ^ b). XOR the numbers with that bit set to get a; b = (a ^ b) ^ a.
Approach 1
class Solution {
public int[] singleNumber(int[] nums) {
int both = 0;
for (int x : nums) both ^= x;
int diff = both & -both;
int a = 0;
for (int x : nums) if ((x & diff) != 0) a ^= x;
return new int[]{a, both ^ a};
}
}
Verdict: Two passes of XOR.
Before you submit
Edge cases and common mistakes
Test these inputs
Negative values
both == Integer.MIN_VALUE (diff is the sign bit, still fine)