Neural Mastery
← Back to Practice

The Lone Number

Difficulty: Easy · Pattern: Bit Manipulation · Concept: General Coding (DSA) — Core Patterns to Drill

Given a non-empty list of integers where every element appears exactly twice except for one, which appears exactly once, find that one element.

A hash-map count gets there in O(n)O(n) time and O(n)O(n) space. Bit manipulation gets the same time complexity with O(1)O(1) space — no auxiliary structure at all — by exploiting one property of XOR (^): xx=0x \oplus x = 0 for any xx, and XOR is commutative and associative, so order doesn't matter.

Your task: implement lone_number(nums) using only XOR — no dictionary, no set.

Implement it yourself
assert lone_number([4, 1, 2, 1, 2]) == 4 assert lone_number([2, 2, 1]) == 1 assert lone_number([7]) == 7 assert lone_number([0, 1, 0]) == 1

Next: The Two Lone Numbers (a harder variant), or continue to The Gap in the Sequence

Last updated Sep 5, 2026Edit this pageReport an issue