LeetCode #260 Medium

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.

Constraints
  • 2 <= nums.length <= 3 * 10⁴
  • -2³¹ <= nums[i] <= 2³¹ - 1
  • Each integer in nums will appear twice, only two integers will appear once.
bit-manipulationarrayxor
Open on LeetCode ↗
02

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: a and b land in different groups, so each group's XOR is one of them.
How to spot this pattern

"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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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].

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def singleNumber(self, nums: List[int]) -> List[int]:
▶3 xor_all = 0
▶4 for x in nums:
▶5 xor_all ^= x
▶6 low = xor_all & -xor_all
▶7 a = b = 0
▶8 for x in nums:
▶9 if x & low:
▶10 a ^= x
▶11 else:
▶12 b ^= x
▶13 return [a, b]
nums100012001010001300112001050101xor of all0001→1lowest 1-bitbit is 1xorbit is 0xorfold in 1 → running XOR 1
x1
xor_all10001
XOR flips exactly the bits where 1 (0001) has a 1, shown in blue: 0 becomes 1. If 1 has a twin later, that copy will flip the same bits back.
nums100012001010001300112001050101xor of all0011→3lowest 1-bitbit is 1xorbit is 0xorfold in 2 → running XOR 3
x2
xor_all30011
XOR flips exactly the bits where 2 (0010) has a 1, shown in blue: 1 becomes 3. If 2 has a twin later, that copy will flip the same bits back.
nums100012001010001300112001050101xor of all0010→2lowest 1-bitbit is 1xorbit is 0xorfold in 1 → running XOR 2
x1
xor_all20010
XOR flips exactly the bits where 1 (0001) has a 1, shown in blue: 3 becomes 2. This is the second copy of 1, so it undoes the flips its first copy made: the pair now counts for nothing.
nums100012001010001300112001050101xor of all0001→1lowest 1-bitbit is 1xorbit is 0xorfold in 3 → running XOR 1
x3
xor_all10001
XOR flips exactly the bits where 3 (0011) has a 1, shown in blue: 2 becomes 1. If 3 has a twin later, that copy will flip the same bits back.
nums100012001010001300112001050101xor of all0011→3lowest 1-bitbit is 1xorbit is 0xorfold in 2 → running XOR 3
x2
xor_all30011
XOR flips exactly the bits where 2 (0010) has a 1, shown in blue: 1 becomes 3. This is the second copy of 2, so it undoes the flips its first copy made: the pair now counts for nothing.
nums100012001010001300112001050101xor of all0110→6lowest 1-bitbit is 1xorbit is 0xorfold in 5 → running XOR 6
x5
xor_all60110
XOR flips exactly the bits where 5 (0101) has a 1, shown in blue: 3 becomes 6. If 5 has a twin later, that copy will flip the same bits back.
nums100012001010001300112001050101xor of all0110→6lowest 1-bit0010→2bit is 1xorbit is 0xorlowest 1-bit is the 2s bit → split on it
xor_all60110
low20010
Every pair cancelled, so 6 is the XOR of the two singles. A 1 in it marks a bit where they differ; the lowest one is the 2s bit. One single has this bit and the other does not, so it can tell them apart.
nums100012001010001300112001050101xor of all0110→6lowest 1-bit0010→2bit is 1xor0bit is 01xor11 has a 0 at the 2s bit → bit-0 group
x10001
b1
1 (0001) has a 0 at the 2s bit, so it goes into the bit-0 group, whose XOR is now 1. Its twin, if it has one, must land in this same group, because equal numbers share every bit.
nums100012001010001300112001050101xor of all0110→6lowest 1-bit0010→2bit is 12xor2bit is 01xor12 has a 1 at the 2s bit → bit-1 group
x20010
a2
2 (0010) has a 1 at the 2s bit, so it goes into the bit-1 group, whose XOR is now 2. Its twin, if it has one, must land in this same group, because equal numbers share every bit.
nums100012001010001300112001050101xor of all0110→6lowest 1-bit0010→2bit is 12xor2bit is 011xor01 has a 0 at the 2s bit → bit-0 group
x10001
b0
1 (0001) has a 0 at the 2s bit, so it goes into the bit-0 group, whose XOR is now 0. Its twin is already in this group, because equal numbers share every bit, so the two cancel.
nums100012001010001300112001050101xor of all0110→6lowest 1-bit0010→2bit is 123xor1bit is 011xor03 has a 1 at the 2s bit → bit-1 group
x30011
a1
3 (0011) has a 1 at the 2s bit, so it goes into the bit-1 group, whose XOR is now 1. Its twin, if it has one, must land in this same group, because equal numbers share every bit.
nums100012001010001300112001050101xor of all0110→6lowest 1-bit0010→2bit is 1232xor3bit is 011xor02 has a 1 at the 2s bit → bit-1 group
x20010
a3
2 (0010) has a 1 at the 2s bit, so it goes into the bit-1 group, whose XOR is now 3. Its twin is already in this group, because equal numbers share every bit, so the two cancel.
nums100012001010001300112001050101xor of all0110→6lowest 1-bit0010→2bit is 1232xor3bit is 0115xor55 has a 0 at the 2s bit → bit-0 group
x50101
b5
5 (0101) has a 0 at the 2s bit, so it goes into the bit-0 group, whose XOR is now 5. Its twin, if it has one, must land in this same group, because equal numbers share every bit.
nums100012001010001300112001050101xor of all0110→6lowest 1-bit0010→2bit is 1232xor3bit is 0115xor5answer: 3 and 5
a3
b5
Each group held one single plus whole pairs, and the pairs cancelled. What is left in the two groups is the answer: [3, 5].
05

Common pitfalls

Negating INT_MIN in C++

✗ Wrong
int xorAll = 0;
for (int x : nums) xorAll ^= x;
int low = xorAll & -xorAll;
✓ Right
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

✗ Wrong
if ((x & low) > 0)
✓ Right
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].

06

Edge cases

Only the two singles, e.g. [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.

Negative numbers in Python, e.g. [-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.

07

Complexity

Time
O(n)
Space
O(1)
Two passes over nums, each doing one XOR or AND per number, and four integer variables in total.
08

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.