Single Number Iii

To solve this coding challenge, the goal is to identify two unique numbers in an array where all other numbers appear exactly twice, in linear time complexity and using constant extra space.

Explanation

To achieve this, we can leverage the properties of the XOR bitwise operator, which has some useful characteristics for identifying unique numbers in a set of pairs:
  1. XOR of a number with itself is zero, i.e.,
    a ^ a = 0
    .
  2. XOR of a number with zero is the number itself, i.e.,
    a ^ 0 = a
    .
  3. XOR is commutative and associative:
    a ^ b ^ a = b
    .
  4. These properties help us to cancel out all numbers that appear in pairs, leaving only the XOR of the two unique numbers.
    Method to Find the Two Unique Numbers:
  5. XOR All Numbers: XOR all elements in the array. Since all numbers that appear twice will cancel out to zero, the result will be the XOR of the two unique numbers (
    result
    =
    num1 ^ num2
    ).
  6. Identify Rightmost Set Bit: Find a bit that is set to
    1
    in the XOR result. This bit is different between the two unique numbers.
  7. Divide and Conquer: Using this bit, divide the array elements into two groups:
    • One group with the bit set.
    • One group with the bit not set.
    • Each group will have one unique number and all other numbers appearing in pairs, allowing us to apply XOR again within these subgroups to find the unique elements.
    Detailed Steps in Pseudocode:
    Below is step-by-step pseudocode detailing every part with comments explaining each step:
  8. Initialize Variables:
  9.                                             
    # Initialize a variable to hold XOR result of all numbers
    xor_result = 0
    
                                            
  10. XOR All Numbers:
  11.                                             
    # Step through each number in nums
    for each number in nums:
    # XOR current number with xor_result
    xor_result = xor_result XOR number
    
                                            
  12. Find Rightmost Set Bit:
  13.                                             
    # Initialize a mask to find the rightmost set bit (1)
    mask = 1
    
    # Loop to find the first set bit in xor_result
    while (mask AND xor_result) == 0:
    # Shift mask left by 1 to check the next bit
    mask = mask LEFT SHIFT 1
    
                                            
  14. Divide into Two Groups and Find Unique Numbers:
  15.                                             
    # Initialize the two unique numbers to 0
    unique_number1 = 0
    unique_number2 = 0
    
    # Step through each number in nums
    for each number in nums:
    # Check if current number matches the set bit in mask
    if (mask AND number) != 0:
    # XOR with the first unique number
    unique_number1 = unique_number1 XOR number
    else:
    # XOR with the second unique number
    unique_number2 = unique_number2 XOR number
    
                                            
  16. Return the Two Unique Numbers:
                                            
# Return the result as a pair in a list
return [unique_number1, unique_number2]

                                        
Putting it all together in a flowing pseudocode:
                                            
# Step 1: Initialize xor_result to 0
xor_result = 0

# Step 2: XOR all numbers in the list
for each number in nums:
    xor_result = xor_result XOR number

# Step 3: Find the rightmost set bit in xor_result
mask = 1
while (mask AND xor_result) == 0:
    mask = mask LEFT SHIFT 1

# Step 4: Initialize two unique numbers to 0
unique_number1 = 0
unique_number2 = 0

# Step 5: Divide numbers into two groups and XOR within each group
for each number in nums:
    if (mask AND number) != 0:
        unique_number1 = unique_number1 XOR number
    else:
        unique_number2 = unique_number2 XOR number

# Step 6: Return the two unique numbers
return [unique_number1, unique_number2]

                                        
This pseudocode effectively captures the logic for finding the two unique numbers in an array using bitwise XOR operations and separation based on a distinguishing set bit, achieving both linear runtime complexity and constant space usage.