Single Number III

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Single Number III

Approach

XOR all elements to get a ^ b where a and b are the two unique numbers. Find the rightmost set bit in a ^ b to use as a splitter, then partition all numbers into two groups by that bit. XOR within each group isolates one unique number per group.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

XOR Pattern

Why It Works

The differing bit guarantees a and b land in separate groups, while all duplicate pairs stay together within the same group and cancel via XOR.

Updated Feb 2026