Single Number III
Single Number III is LeetCode 260 (Medium). In nums, exactly two numbers appear once and every other number appears exactly twice.
- Return the two values that appear once, in any order.
- The solution must run in O(n) time with O(1) extra space, so a hash map of counts is not allowed.
Values span the full 32-bit range, negatives included, which matters in C++ and Java.
- 2 <= nums.length <= 3 * 10⁴
- -2³¹ <= nums[i] <= 2³¹ - 1
- Each integer in nums will appear twice, only two integers will appear once.
Intuition
A number XOR itself is 0, so XOR-ing the whole array cancels every pair and leaves a ^ b, the XOR of the two singles. But a and b are different, so a ^ b has at least one 1-bit, and at that position one of them has a 1 and the other has a 0.
- Split by that bit: each number joins either the bit-is-1 group or the bit-is-0 group.
- Pairs stay together: both copies of a value have the same bit, so they land in the same group and cancel.
- The singles separate:
aandbland in different groups, so each group's XOR is one of them.
"Everything appears twice except a few" together with O(1) space points to XOR. When the XOR mixes two answers together, look for a bit that tells them apart and split on it.
Approach
Before reading on, XOR a small example by hand and ask what the result says about the two singles. Then find a way to separate them with one more pass. Aim for O(n) time and a handful of variables.
XOR the whole array
Start xor_all at 0 and XOR in every number. XOR does not care about order, so the two copies of each value cancel wherever they sit, and the total is a ^ b.
Keep only the lowest 1-bit
Compute low = xor_all & -xor_all. In two's complement, -x flips every bit of x above its lowest 1-bit and keeps that bit itself, so the AND leaves a number with exactly that one bit set: a position where a and b differ.
Split the numbers and XOR each group
Walk nums again. XOR each number into a if x & low is non-zero, and into b otherwise. Each group holds one single plus whole pairs, so when the walk ends, a and b are the two answers. Return [a, b].
Code in Python, C++ and Java
Common pitfalls
Negating INT_MIN in C++
int xorAll = 0; for (int x : nums) xorAll ^= x; int low = xorAll & -xorAll;
unsigned xorAll = 0; for (int x : nums) xorAll ^= x; unsigned low = xorAll & -xorAll;
If the two singles differ only in the sign bit, as in [-2147483641, 7], xorAll is -2147483648. Negating it overflows int, which is undefined behaviour. LeetCode's sanitizer stops the run with "negation of -2147483648 cannot be represented in type 'int'". Negating an unsigned is defined and keeps the same bit.
Testing the bit with > 0 in Java
if ((x & low) > 0)
if ((x & low) != 0)
When low is the sign bit, x & low is -2147483648 for every number that has it, and that is not greater than 0. On [-2147483641, 7] both numbers fall into the same group and the method returns [0, -2147483648].
Edge cases
[0, 1]There are no pairs: xor_all = 1 and low = 1. The 1 goes into the bit-set group and the 0 into the other, so the bit-clear group's XOR stays 0, and 0 is one of the real answers.
[-1, 0]Python integers have no fixed width, but negative numbers behave as if they had infinitely many leading 1-bits, so x & -x still gives the lowest set bit (here 1) and the split works unchanged.
Complexity
nums, each doing one XOR or AND per number, and four integer variables in total.Single Number III FAQ
Why does any 1-bit of a ^ b work, not just the lowest?
Every 1 in a ^ b marks a position where a and b differ, so any of them splits the two apart. The lowest one is used only because x & -x isolates it in a single step.
How is this different from Single Number and Single Number II?
Single Number has one single among pairs, so XOR of everything is already the answer. Single Number II has one single among triples, where XOR fails and you count each bit modulo 3 instead. Here there are two singles among pairs, so XOR gives a ^ b and one more pass is needed to split them.