LeetCode ยท #136

Single Number Solution

Every element appears twice except one. Find the single element in O(n) time and O(1) space.

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Easy

๐Ÿ”—

LeetCode

Problem #136

๐Ÿ—๏ธ

Pattern

Bit Manipulation โ€” XOR

Given a non-empty array where every element appears twice except one, find that single one. Must run in O(n) time and O(1) extra space.

Example
Input: nums = [4,1,2,1,2] Output: 4
Constraints
โ€ข 1 โ‰ค nums.length โ‰ค 3 ร— 10โด โ€ข Each element appears twice except one
02
Section Two ยท Approach 1

HashMap โ€” O(n) time, O(n) space

Count frequencies. Return the element with count 1.

Problem: O(n) space. XOR gives O(1) space because a โŠ• a = 0 and a โŠ• 0 = a. All duplicates cancel out, leaving only the single number.
Java โ€” HashMap
import java.util.*; class Solution { public int singleNumber(int[] nums) { Map<Integer,Integer> map = new HashMap<>(); for (int n : nums) map.merge(n, 1, Integer::sum); for (var e : map.entrySet()) if (e.getValue() == 1) return e.getKey(); return -1; } }
Python โ€” Counter
from collections import Counter class Solution: def singleNumber(self, nums: list[int]) -> int: return [k for k, v in Counter(nums).items() if v == 1][0]
03
Section Three ยท Approach 2

XOR โ€” O(n) time, O(1) space

XOR all elements. Since a โŠ• a = 0 and a โŠ• 0 = a, all duplicates cancel out, leaving only the single number.

๐Ÿ’ก Mental model: Imagine a light switch. Each number "flips" a switch. If a number appears twice, it flips on then off โ€” canceling out. After all flips, the only switch still on is the single number. XOR is that light switch โ€” same value twice returns to zero.
๐ŸŽฏ When to recognize this pattern: "Find the element that appears odd times when all others appear even times." XOR is the tool. Key properties: commutative, associative, self-inverse. Also used in LC 268 (Missing Number), LC 137 (Single Number II โ€” bit counting for triples), LC 260 (Single Number III โ€” two unique numbers).
04
Section Four ยท Trace

Visual Walkthrough

XOR โ€” Duplicates Cancel
nums = [4, 1, 2, 1, 2]. XOR all: result = 0. 0 โŠ• 4 = 4 โ†’ 4 โŠ• 1 = 5 โ†’ 5 โŠ• 2 = 7 โ†’ 7 โŠ• 1 = 6 โ†’ 6 โŠ• 2 = 4 Pairs cancel: (1โŠ•1)=0, (2โŠ•2)=0, leaving 4. return 4 โœ“
05
Section Five ยท Implementation

Code โ€” Java & Python

Java โ€” XOR
class Solution { public int singleNumber(int[] nums) { int res = 0; for (int n : nums) res ^= n; return res; } }
Python โ€” XOR (reduce)
from functools import reduce from operator import xor class Solution: def singleNumber(self, nums: list[int]) -> int: return reduce(xor, nums)
06
Section Six ยท Analysis

Complexity Analysis

ApproachTimeSpace
HashMapO(n)O(n)
Sort + scanO(n log n)O(1)
XOR โ† optimalO(n)O(1)
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
Single element[42]0 โŠ• 42 = 42.
Negative numbers[-1,1,-1]XOR works on negative ints (two's complement).
Zero is single[0,1,1]0 is valid. XOR: 0โŠ•1โŠ•1 = 0.
โš  Common Mistake: Trying to solve with sum(set) * 2 - sum(array). This works mathematically but can overflow for large values. XOR has no overflow risk and is the cleanest solution.

โ† Back to Math & Bit problems