Single Number II

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 II

Approach

For each of the 32 bit positions, sum that bit across all numbers. Take the sum modulo 3 to isolate the unique element's bit at that position. Reconstruct the result by OR-ing each surviving bit back into place.

Complexity Analysis

Time
O(32n)
Space
O(1)

Pattern

Bit Counting

Why It Works

Every number appearing three times contributes a multiple of 3 to each bit position sum, so mod 3 eliminates their contribution and reveals only the single element's bits.

Updated Feb 2026