Problem
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
- Given nums =
[1, 2, 1, 3, 2, 5]
, return[3, 5]
.Note:
- The order of the result is not important. So in the above example,
[5, 3]
is also correct.- Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
Idea
Suppose C = A ^ B
, then the 1
bits in C
are exactly the ones that only exist in either A
or B
(C
cannot be 0
or A
and B
would be the same). It is because all other elements appearing twice are turned into 0
during the XOR process. The XOR result C
is actually the XOR of the only two elements each appearing once.
Thus, for whichever 1
bit in C
, having it or not
can be the condition for dividing those elements into Group A
(which containing A
) and Group B
(which containing B
). As per to the explanation above, the XOR result of those elements in Group A
(except for A
) would be 0
, and the XOR of all elements in Group A
would be A
. It’s the same story in Group B
.
Java Code
|
|
- Trick of getting the lowest
1
bit of C
– For a positive integerC
,-C
would be stored as2's complement
ofC
and with the highest bit (sign bit) set to1
. To compute the2's complement
of an integer, simply keep the trailing0
s and the lowest1
bit unchanged, and then revert all the higher bits. So,C & (-C)
would be the lowest1
bit ofC
.