Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Easy Bit Manipulation High frequency Solution Available Practice Mode

Single Number LeetCode Solution

Tags: #Array #Bit Manipulation

Approach Summary

XOR all numbers. Pairs cancel out (x ^ x = 0). The result is the single number.

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Bit Manipulation Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Bit Manipulation pattern.

Framework: Bit Manipulation • Interview Prep: Tested at FAANG • Explore all Bit Manipulation problems & invariants →

How to Recognize This Pattern

  • Find element appearing once, all others twice
  • XOR trick

Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)

Tags

Array Bit Manipulation

Frequently Asked Questions

What is the optimal time and space complexity for Single Number?
The optimal solution for Single Number achieves O(n) runtime complexity and requires O(1) auxiliary space. One pass with a constant-time XOR per element — O(n) time. A single integer variable — O(1) space.
How do I identify that Single Number requires the Bit Manipulation pattern?
In technical interviews, look for key problem signals: Find element appearing once, all others twice; XOR trick. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Bit Manipulation strategy.
What critical edge cases must be handled in Single Number?
Essential edge cases to verify include: Single element — the singleton itself; Negative numbers — XOR works identically on two's-complement integers; Large arrays — no overflow risk with XOR, unlike sum-based tricks. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Single Number?
Single Number is a Easy-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Apple, Bloomberg.
Buy me a coffee
Daily Practice
0/0