Single Number III - Problem

Imagine you have an array of integers where exactly two elements appear only once and all other elements appear exactly twice. Your mission is to find these two unique elements!

This problem is a fascinating extension of bit manipulation techniques. While finding one unique number among duplicates is straightforward using XOR, finding two unique numbers requires a clever twist.

The Challenge: You must solve this in O(n) time complexity and O(1) space complexity - no hash tables allowed!

Example: Given [1,2,1,3,2,5], the two unique numbers are 3 and 5.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,1,3,2,5]
โ€บ Output: [3,5]
๐Ÿ’ก Note: Numbers 1 and 2 appear twice each, while 3 and 5 appear only once. The algorithm uses XOR to find that 3โŠ•5=6, then uses bit 2 (010) to separate them into different groups.
example_2.py โ€” Negative Numbers
$ Input: nums = [-1,0]
โ€บ Output: [-1,0]
๐Ÿ’ก Note: Both -1 and 0 appear only once. The XOR of all elements is -1โŠ•0=-1, and the distinguishing bit separates them correctly.
example_3.py โ€” Larger Numbers
$ Input: nums = [0,1,0,3,0,4,0,4,3]
โ€บ Output: [1,3]
๐Ÿ’ก Note: Wait, this violates the constraint! Let's use: nums = [0,1]. Here 0 and 1 both appear once, so XOR gives 0โŠ•1=1, and bit 1 separates them.

Visualization

Tap to expand
๐Ÿ” The XOR Detective Method121325Input: Duplicates (faded) + Two Unique (bold)Step 1: XOR Magic โšก1โŠ•2โŠ•1โŠ•3โŠ•2โŠ•5= (1โŠ•1)โŠ•(2โŠ•2)โŠ•3โŠ•5= 0โŠ•0โŠ•3โŠ•5 = 3โŠ•5 = 6Combined: 110โ‚‚Step 2: Find the Distinguishing Bit ๐Ÿ”6 & (-6) = 110โ‚‚ & 010โ‚‚ = 010โ‚‚Rightmost set bit = 2 (position 1)This bit differs between 3 and 5!Binary Detective Work ๐Ÿ•ต๏ธ3 = 011โ‚‚5 = 101โ‚‚Diff: 010โ‚‚ โ† Key!Bit 1 separates themStep 3: Divide into Groups ๐Ÿ‘ฅGroup A (bit 1 set):3 (011โ‚‚) โœ“XOR: 3Group B (bit 1 unset):1,2,1,2,5XOR: 5๐ŸŽฏ Found Them![3, 5]
Understanding the Visualization
1
Combine All Fingerprints
XOR all numbers - duplicates cancel out, leaving only the XOR of our two targets
2
Find the Key Difference
The rightmost set bit in our XOR result shows us how the two numbers differ
3
Divide and Conquer
Split the array based on this distinguishing bit - each unique number goes to a different group
4
Extract the Individuals
XOR each group separately to eliminate pairs and reveal the unique number in each group
Key Takeaway
๐ŸŽฏ Key Insight: XOR eliminates duplicate pairs and reveals the combined signature of unique numbers. The rightmost set bit in this signature is our 'fingerprint scanner' that perfectly separates the two unique suspects!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through array to XOR all elements, then another pass to separate groups

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few integer variables for XOR operations and bit manipulation

n
2n
โœ“ Linear Space

Constraints

  • 2 โ‰ค nums.length โ‰ค 3 ร— 104
  • -231 โ‰ค nums[i] โ‰ค 231 - 1
  • Each integer appears exactly twice except for two integers
  • The two unique integers are guaranteed to be different
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 24 Apple 19
84.5K Views
Medium-High Frequency
~18 min Avg. Time
3.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen