15% off all plans & roadmaps Use code
See plansLeetCode 136 · Solution & editorial
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
Single Number is a Bit Manipulation Patterns problem, in the Bitwise XOR - Finding Single/Missing Number subpattern. The pattern covers XOR tricks, counting bits, power-of-two checks, and Hamming weight
Use XOR properties to find single number, missing number, and duplicates.
The reference solution runs in O(n) time and O(1) space.
Single Number has been reported in interviews at Google, Amazon, Facebook, Microsoft.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Single Number is a Bit Manipulation Patterns problem, in the Bitwise XOR - Finding Single/Missing Number subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.