Single Number III
Find the two numbers that appear once in an array where every other number appears twice.
Given an array of integers nums
Example 1
Output: [3, 5]
Example 2
Output: [-1, 0]
The Idea
Picture a drawer of gloves where every glove has its partner except two odd ones. Example 1 is such a drawer: in [1, 2, 1, 3, 2, 5]
Working on the bits of the numbers gives a way to make pairs vanish. XOR, written ^3 ^ 5
Three facts about XOR matter here: a number XORed with itself gives 0, XOR with 0 changes nothing, and the order of the numbers does not change the result. XOR all six numbers of Example 1 together, and the 1s cancel, the 2s cancel, and 3 ^ 5 = 6 is left.
With one lonely number, as in
Think about what a 1 bit in 6 means: at that position, 3 and 5 have different bits. Look at the picture below.
In the column labelled bit 1, 3 has a 1 and 5 has a 0. Now split all six numbers by that bit alone. The box labelled "bit 1 is 1" gets 2, 3 and 2, and the box labelled "bit 1 is 0" gets 1, 1 and 5. Both 2s land in the same box, because equal numbers have equal bits, and so do both 1s.
Inside each box the pairs cancel again, so the XOR of the group is 3 in one box and 5 in the other. Because 3 and 5 differ at bit 1, they always end up in different boxes. Each box then holds just one lonely number, and XOR finds it.
Solution Approach
Counting each number would be the simple way, but it does not fit the memory limit. The XOR idea needs three decisions instead: how to combine all the numbers, how to pick one bit where the two lonely numbers differ, and how to split the numbers by it.
Counting with a map
Why not just count? A HashMapint
Combining all the numbers
What should the first pass compute? Start a variable xorxor
That value is never 0. The two lonely numbers are different, so they differ in at least one bit, and that bit is 1 in xor
Picking one differing bit
Which 1 bit of xorxor & -xor&xor-xor
Why does -xor-xorxorxor = 6
| Value | Last 8 bits (bit 7 to bit 0) |
|---|---|
| xor = 6 | 0000 0110 |
| every bit flipped | 1111 1001 |
| -xor, the flipped bits plus 1 | 1111 1010 |
| xor & -xor | 0000 0010 |
Adding 1 to the flipped bits turns their trailing 1s back into 0s and stops at the first 0. That 0 sits exactly where xor-xorxor
So only the lowest 1 bit survives the &
Does this hold when the top bit, the sign bit, is the one that remains? If xorInteger.MIN_VALUEintxor & -xor
Splitting the numbers
How is the split done without building two lists? A second pass tests each number with num & lowestlowesta
The other box needs no pass of its own. Since xorxor ^ aa
Java Solution
The method singleNumber
- First, XOR every number into
, which starts at 0. When this first loop ends, the pairs have cancelled andxor holds the two lonely numbers XORed together.xor - Keep only the lowest 1 bit of
inxor , withlowest .xor & -xor - Loop over the numbers a second time. Each number with
other than 0 is XORed intonum & lowest , which starts at 0. When this loop ends,a is the lonely number of that box.a - Return
anda , the other lonely number.xor ^ a
The mainInteger.MAX_VALUE
import java.util.Arrays;
public class SingleNumberIII {
public static int[] singleNumber(int[] nums) {
int xor = 0;
for (int num : nums) {
xor ^= num; // pairs cancel: the two lonely numbers are left
}
int lowest = xor & -xor; // lowest 1 bit: the lonely numbers differ there
int a = 0;
for (int num : nums) {
if ((num & lowest) != 0) { // the group whose numbers have that bit set
a ^= num; // pairs in the group cancel, one answer is left
}
}
return new int[] {a, xor ^ a}; // xor ^ a cancels a, leaving the other one
}
public static void main(String[] args) {
System.out.println(Arrays.toString(singleNumber(new int[] {1, 2, 1, 3, 2, 5})));
System.out.println(Arrays.toString(singleNumber(new int[] {-1, 0})));
System.out.println(Arrays.toString(singleNumber(new int[] {4, -6, 4, -9, 8, 8})));
System.out.println(Arrays.toString(
singleNumber(new int[] {Integer.MAX_VALUE, 9, -1, 9})));
}
}
Running it prints:
[3, 5]
[-1, 0]
[-9, -6]
[-1, 2147483647]
Example 1 prints [3, 5]a[-1, 0]xorlowest[-1, 0]
The third line is [-9, -6][-1, 2147483647]xorInteger.MIN_VALUElowest
Why split by one bit and not by xornum & xor[6, 0][3, 5]
Complexity
The method reads the array twice and keeps three int
- Time: O(n), because each of the two loops visits the n numbers once and does one XOR or one bit test per number.
- Space: O(1) besides the two-number result, because the method keeps only
,xor andlowest , where a counting map would grow with the number of different values.a