AlgoMaster Logo

Single Number

easyFrequency5 min readUpdated June 23, 2026

Understanding the Problem

We have an array where every number shows up exactly twice, except for one number that appears only once. Our job is to find that unique number.

Finding which number lacks a pair is easy. The constraint is what makes the problem interesting: we need O(n) time and O(1) space. That rules out sorting (O(n log n)) and hash maps (O(n) space).

The problem is about pairing. Every number has a partner except one. If we can cancel out all the pairs, only the answer remains.

Key Constraints:

  • nums.length <= 3 * 10^4 → An O(n^2) scan is around 9 * 10^8 comparisons in the worst case, slow enough to time out. We want O(n).
  • -3 * 10^4 <= nums[i] <= 3 * 10^4 → Values can be negative, so index-based counting tricks won't work directly.
  • Every element appears exactly twice except one → This guarantee is what lets pairs cancel cleanly.

Approach 1: Brute Force (Nested Loops)

Intuition

For each element, check whether it has a duplicate somewhere else in the array. If no duplicate exists, that element is the answer.

We pick each element one by one and scan the rest of the array looking for a match. If we finish scanning without finding one, we have found the single number.

Algorithm

  1. For each element nums[i], set a flag indicating no duplicate has been found.
  2. For each other element nums[j] where j != i, check if nums[i] == nums[j].
  3. If a match is found, mark that a duplicate exists and break the inner loop.
  4. If no match was found after the inner loop completes, return nums[i].

Example Walkthrough

1Check each element for a duplicate elsewhere in the array
0
1
1
2
2
1
3
3
4
2
1/6

Code

The cost here is the repeated scanning: every element triggers another pass over the array. The next approach removes that by counting occurrences in a single pass.

Approach 2: Hash Map (Count Occurrences)

Intuition

Instead of rescanning the array for every element, count how many times each number appears using a hash map, then return the number whose count is 1.

This trades space for time. One pass builds the frequency map, a second pass finds the element that appears once. Both passes are linear.

Algorithm

  1. Create a hash map to store the count of each number.
  2. Iterate through the array. For each number, increment its count in the map.
  3. Iterate through the map and return the number whose count is 1.

Example Walkthrough

1Initialize: empty map = {}
0
4
1
1
2
2
3
1
4
2
1/7

Code

This uses O(n) extra space for the hash map, but the problem asks for O(1) space. The next approach cancels out paired elements without storing anything, leaving only the unpaired one.

Approach 3: XOR (Bit Manipulation)

Intuition

The XOR operator has three properties that solve this problem in one pass with no extra storage:

  1. Self-cancellation: a ^ a = 0 (any number XOR'd with itself becomes 0)
  2. Identity: a ^ 0 = a (any number XOR'd with 0 stays the same)
  3. Commutativity and Associativity: The order doesn't matter. a ^ b ^ a = a ^ a ^ b = 0 ^ b = b

XOR all elements together. Because order is irrelevant, the two copies of each paired number sit next to each other in effect and cancel to 0. The single number XORs with 0 and survives. For [a, a, b, b, c], the running XOR reduces to (a ^ a) ^ (b ^ b) ^ c = 0 ^ 0 ^ c = c.

Algorithm

  1. Initialize a variable result to 0.
  2. XOR every element in the array with result.
  3. After the loop, result holds the single number.

Example Walkthrough

1Initialize: result = 0
0
4
1
1
2
2
3
1
4
2
1/7

Code