2Brute2Best
🔥0

Single Number

Easy·LeetCode #136Basic Anchor
LeetCode
30-Second Mental ModelXOR cancellation (single number)
Complexity:O(N²)→O(N)
Key Insight

Start with ans = 0 and XOR every number into it. Two equal numbers cancel, because x ^ x = 0. Zero changes nothing, because x ^ 0 = x, so the single value stays. The order does not matter, so the pairs do not need to sit next to each other.

Core Validity Invariant

The running value is valid when it equals the XOR of every number read so far. At the end, every repeated value has appeared twice and cancelled, so only the single value is left. Pairs cancel out, whatever the order.

01

Problem

You get an array nums. Every value in it appears twice, except for one value that appears only once. Return that single value.

The numbers are in no particular order. Your solution must run in linear time and use constant extra space.

Input

An integer array nums with at least one value. Every value appears twice except one.

Output

The one value that appears exactly once.

Example 1
Input: nums = [2, 2, 1]
Output: 1

The 2 shows up twice, so the value with no partner is 1.

Example 2
Input: nums = [4, 1, 2, 1, 2]
Output: 4

1 and 2 each show up twice. The 4 is the only value without a partner.

Example 3
Input: nums = [1]
Output: 1

There is only one value, so it is the single one.

Constraints
  • • 1 ≤ nums.length ≤ 3 · 10^4
  • • −3 · 10^4 ≤ nums[i] ≤ 3 · 10^4
  • • Every value appears twice, except one value that appears once.
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    If I counted how many times each value appears, which count would I be looking for?

  2. 2.

    Can I answer with a simple yes/no check, like 'is this value in the array?' Why or why not?

  3. 3.

    What does XOR give when I combine a number with itself? And with 0?

  4. 4.

    If I XOR every number in the array together, which numbers should disappear?

  5. 5.

    Does the order of the numbers change the result of a chain of XORs?

03

Brute force approach

04

Better approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceFor each value, scan the array and count it; return the one with count 1O(N²)O(1)
BetterCount every value once in a hash map, then return the value with count 1O(N)O(N)
OptimalXOR every number into one running value; the pairs cancel and the single one is leftO(N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “every element appears twice except one”Values come in pairs, so pairs must cancel out. That is what XOR does.
  • “linear time, constant extra space”No hash map and no sorting. Read the array once, with one running value.
  • “find the one that appears once”The answer is whatever is left after every pair has cancelled.
Pairs of equal values+XOR of a pair → 0+XOR of everything → the single value→XOR cancellation (single number)

This is the anchor for the XOR sub-pattern. XOR works because a pair cancels itself and 0 is the neutral value. The next questions change the setup. Decode XORed Array uses XOR to undo one step. Single Number III has two singles, so one XOR total mixes them together. Single Number II has triples, which XOR does not cancel.

10

Don't make this mistake

Using the XOR-all trick when the others appear three times
✗ int ans = 0; for (int x : nums) ans ^= x; // on [2, 2, 3, 2]
✓ Count the 1s in each bit position, then keep count % 3 (see Single Number II).

This is the pattern-confusion trap. On [2, 2, 3, 2] the XOR is 2 ^ 2 ^ 3 ^ 2 = 1, but the answer is 3. The first two 2s cancel, leaving 3, and the third 2 XORs it to 1. Three equal values do not cancel: x ^ x ^ x = x.

Starting the running value at nums[0] or at 1
✗ int ans = nums[0]; for (int x : nums) ans ^= x;
✓ int ans = 0; for (int x : nums) ans ^= x;

nums[0] gets XORed in twice, once as the start and once in the loop, so it cancels itself. On [4, 1, 2, 1, 2] this gives 4 ^ 4 ^ 1 ^ 2 ^ 1 ^ 2 = 0, not 4. Starting at 1 is also wrong: it adds a 1 to the result (1 ^ 4 = 5). The neutral start is 0.

Comparing neighbours (this needs a sorted array)
✗ for (int i = 0; i + 1 < n; i += 2) if (nums[i] != nums[i + 1]) return nums[i];
✓ int ans = 0; for (int x : nums) ans ^= x; return ans;

This only works if equal values sit next to each other, which needs a sort first (O(N log N)). On [1, 2, 1, 2, 4] the first two numbers differ, so it returns 1. The answer is 4.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.