Challenges
Last updated
Last updated
Objective:
We need to find the single number that appears exactly once in an array where every other number appears exactly three times.
Key Insight:
Using bitwise operations, we can track the occurrence of each bit across all numbers. By maintaining counters for bits that appear once and twice, we can effectively isolate the bits of the number that appears exactly once.
Bitwise Operations:
ones
: Tracks bits that have appeared once.
twos
: Tracks bits that have appeared twice.
Core Bitwise Formulas:
ones = (ones ^ num) & ~twos
Explanation:
ones ^ num
: Adds the bits of num
to ones
if they are not already there, or removes them if they are.
& ~twos
: Ensures that if a bit is already in twos
, it is removed from ones
. This keeps track of bits that have only appeared once but not yet twice.
twos = (twos ^ num) & ~ones
Explanation:
twos ^ num
: Adds the bits of num
to twos
if they are not already there, or removes them if they are.
& ~ones
: Ensures that if a bit is already in ones
, it is removed from twos
. This keeps track of bits that have appeared twice but not yet three times.
Initialize:
ones = 0
twos = 0
Iterate through the array:
For each number num
in the array, update ones
and twos
:
First Occurrence of a Bit:
Suppose num = 5
(binary 0101
).
Initially, ones = 0
and twos = 0
.
Update:
ones = (ones ^ num) & ~twos
ones = (0 ^ 0101) & ~0000 = 0101 & 1111 = 0101
twos = (twos ^ num) & ~ones
twos = (0 ^ 0101) & ~0101 = 0101 & 1010 = 0000
Second Occurrence of the Same Bit:
When num = 5
appears again:
Update:
ones = (ones ^ num) & ~twos
ones = (0101 ^ 0101) & ~0000 = 0000 & 1111 = 0000
twos = (twos ^ num) & ~ones
twos = (0000 ^ 0101) & ~0000 = 0101 & 1111 = 0101
Third Occurrence of the Same Bit:
When num = 5
appears the third time:
Update:
ones = (ones ^ num) & ~twos
ones = (0000 ^ 0101) & ~0101 = 0101 & 1010 = 0000
twos = (twos ^ num) & ~ones
twos = (0101 ^ 0101) & ~0000 = 0000 & 1111 = 0000
At the end of this process, ones
and twos
are reset to 0 for any number that appears exactly three times.
Extract the Single Occurrence:
The bits of the number that appears exactly once will remain in ones
since it won't be canceled out after three appearances.
Thus, after processing all numbers, ones
will contain the single number.
Let's run through a full example with an array [2, 2, 3, 2]
:
Initialize: ones = 0
, twos = 0
First number 2
(binary 10
):
ones = (0 ^ 10) & ~0 = 10
twos = (0 ^ 10) & ~10 = 0
Second number 2
(binary 10
):
ones = (10 ^ 10) & ~0 = 0
twos = (0 ^ 10) & ~0 = 10
Third number 2
(binary 10
):
ones = (0 ^ 10) & ~10 = 0
twos = (10 ^ 10) & ~0 = 0
Fourth number 3
(binary 11
):
ones = (0 ^ 11) & ~0 = 11
twos = (0 ^ 11) & ~11 = 0
Finally, ones = 3
which is the number that appears exactly once.
=> Remember this code
This approach effectively uses bitwise operations to isolate the single occurrence of a number in an array where every other number appears three times. It works in linear time and uses constant space, making it an efficient solution to the problem.