Single Number Ii

To solve this coding challenge, we need to find an element in the given integer array
nums
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)).
# 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:
  1. Initialization : We start by initializing two variables,
    ones
    and
    twos
    , to zero. These will help us track bits that have appeared once and twice respectively.
  2. Iterating Through the Array : We iterate through each number in the array and update the
    ones
    and
    twos
    variables based on the number.
  3. Bitwise Operations :
    • ones
      is updated to track bits where we have seen the bit appear an odd number of times. This includes the first and third time.
    • twos
      is updated to track bits where we have seen the bit appear twice.
  4. Clearing the Bits : After updating
    ones
    and
    twos
    , we need to clear bits that appear in both
    ones
    and
    twos
    . This is because such bits correspond to numbers that have appeared three times.
  5. Returning the Result : By the end of the iteration,
    ones
    will hold the bit representation of the number that appears exactly once.
  6. # Detailed Steps in Pseudocode
  7. Initialize
    ones
    and
    twos
    to zero.
  8. Iterate through each number in the array.
    • Compute
      new_ones
      to track bits that have appeared an odd number of times.
    • Compute
      new_twos
      to track bits that have appeared twice.
    • Update
      ones
      to be the new value of
      new_ones
      .
    • Update
      twos
      to be the new value of
      new_twos
      .
  9. Clear bits from
    ones
    and
    twos
    when a bit appears in both.
  10. Return
    ones
    , which contains the number that appears exactly once.
  11. Here’s the complete pseudocode for the solution with comments:
                                                
    # 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
    
                                            
    # Step-by-Step Explanation of the Pseudocode
  12. Initialization :
    • ones = 0
    • twos = 0
  13. Loop through each number :
    • For each number, update
      new_ones
      using bitwise XOR and AND operations:
      • (ones ^ num)
        computes the XOR of
        ones
        and
        num
        , flipping bits where
        num
        has 1.
      • ~twos
        computes the bitwise complement of
        twos
        , ensuring that only bits not already in
        twos
        are affected.
      • The result is the new candidate for the bits in
        ones
        .
    • Update
      new_twos
      similarly:
      • (twos ^ num)
        computes the XOR of
        twos
        and
        num
        , flipping bits where
        num
        has 1.
      • ~new_ones
        ensures that bits already in
        ones
        are not included.
    • Assign
      new_ones
      to
      ones
      , and
      new_twos
      to
      twos
      .
  14. Result after loop :
    • After all iterations,
      ones
      will exclusively hold the bits of the number that appears once.
By following these detailed steps, you should be able to implement a solution that correctly identifies the single unique number in a list where every other number appears three times, efficiently and with constant space complexity.