Single Number

Easy
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

Approach

XOR all elements together in a single pass. Since XOR is self-inverse (a ^ a = 0) and commutative, every duplicate pair cancels out, leaving only the unique element.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

XOR Pattern

Why It Works

XOR of a number with itself is zero, and XOR with zero is the number itself, so all paired duplicates vanish and the lone element remains.

Updated Mar 2026