Single Number Ii
To solve this coding challenge, we need to find an element in the given integer array
that appears exactly once, while every other element appears three times. We need an approach that operates with linear runtime complexity (O(n)) and uses only constant extra space (O(1)).
nums
# Explanation
The problem asks for a solution that works efficiently and using constant space. A very effective way to approach problems where elements repeat a specific number of times is using bitwise operations. Bitwise operations allow us to manipulate individual bits of numbers directly and are typically very fast. The core idea behind the solution is using two variables to track the count bits for the numbers seen so far. Here's how it works step-by-step:-
Initialization
: We start by initializing two variables,
and
ones, to zero. These will help us track bits that have appeared once and twice respectively.twos -
Iterating Through the Array
: We iterate through each number in the array and update the
and
onesvariables based on the number.twos - Bitwise Operations :
-
is updated to track bits where we have seen the bit appear an odd number of times. This includes the first and third time.
ones -
is updated to track bits where we have seen the bit appear twice.
twos -
Clearing the Bits
: After updating
and
ones, we need to clear bits that appear in bothtwosandones. This is because such bits correspond to numbers that have appeared three times.twos -
Returning the Result
: By the end of the iteration,
will hold the bit representation of the number that appears exactly once.
ones -
Initialize
and
onesto zero.twos - Iterate through each number in the array.
-
Compute
to track bits that have appeared an odd number of times.
new_ones -
Compute
to track bits that have appeared twice.
new_twos -
Update
to be the new value of
ones.new_ones -
Update
to be the new value of
twos.new_twos -
Clear bits from
and
oneswhen a bit appears in both.twos -
Return
, which contains the number that appears exactly once.
ones
Here’s the complete pseudocode for the solution with comments:
- Initialization :
-
ones = 0 -
twos = 0 - Loop through each number :
-
For each number, update
using bitwise XOR and AND operations:
new_ones -
computes the XOR of
(ones ^ num)andones, flipping bits wherenumhas 1.num -
computes the bitwise complement of
~twos, ensuring that only bits not already intwosare affected.twos -
The result is the new candidate for the bits in
.
ones -
Update
similarly:
new_twos -
computes the XOR of
(twos ^ num)andtwos, flipping bits wherenumhas 1.num -
ensures that bits already in
~new_onesare not included.ones -
Assign
to
new_ones, andonestonew_twos.twos - Result after loop :
-
After all iterations,
will exclusively hold the bits of the number that appears once.
ones
# Detailed Steps in Pseudocode
# Pseudocode for finding the single number that appears exactly once
# Initialize two variables to store counts of bits appearing once and twice
ones = 0
twos = 0
# Iterate through each number in the input array nums
for num in nums:
# Update ones to include the current num only if it has not been included twice
new_ones = (ones ^ num) & ~twos
# Update twos to include the current num only if it has not been included once
new_twos = (twos ^ num) & ~new_ones
# Assign the new values to ones and twos for the next iteration
ones = new_ones
twos = new_twos
# The number that appears exactly once will be in ones
return ones