Skip to content
Veltoryx Technologies
FreeProblem 2● EasyLeetCode #136 ↗2 min readUpdated 30 October 2026

Single Number

Every number appears twice except one. Find it in one pass with no extra memory, using XOR.

01The problem

In nums, every value appears exactly twice except one, which appears once. Return that single value. You must use O(n) time and O(1) extra space.

InputOutput
[2, 2, 1]1
[4, 1, 2, 1, 2]4
[1]1

02How to spot it

"Everything appears twice except one" plus "no extra memory" is the classic hint for XOR (^ in Java). A HashSet would work too, but it uses O(n) memory.

03Pseudocode

text
result = 0
for each n in nums:
    result = result XOR n
return result

Two facts make this work:

  • x XOR x = 0: a number cancels itself out.
  • x XOR 0 = x: XOR with zero changes nothing.

And XOR doesn't care about order, so every pair cancels, and only the single number is left.

04Watch it work

Visualizer · step through the algorithmnums = [4,1,2,1,2]
40
11
22
13
24

result

0

binary 000

Start with result = 0. XOR-ing anything with 0 leaves it unchanged.

Pseudocode

  1. result = 0
  2. for each n in nums:
  3. result = result XOR n
  4. return result

Variables

result
0
step 1 / 7

05Step-by-step dry run

  1. 1
    40
    11
    22
    13
    24

    result

    0

    binary 000

    Start with result = 0. XOR-ing anything with 0 leaves it unchanged.

    result = 0

  2. 2
    40
    11
    22
    13
    24
    i

    result

    4

    000 ⊕ 100 = 100

    Mix in 4: 0 ⊕ 4 = 4. Its bits are switched on in result for now.

    n = 4result = 4

  3. 3
    40
    11
    22
    13
    24
    i

    result

    5

    100 ⊕ 001 = 101

    Mix in 1: 4 ⊕ 1 = 5. Its bits are switched on in result for now.

    n = 1result = 5

  4. 4
    40
    11
    22
    13
    24
    i

    result

    7

    101 ⊕ 010 = 111

    Mix in 2: 5 ⊕ 2 = 7. Its bits are switched on in result for now.

    n = 2result = 7

  5. 5
    40
    11
    22
    13
    24
    i

    result

    6

    111 ⊕ 001 = 110

    1 again: XOR flips the same bits back, so the pair cancels out. 7 ⊕ 1 = 6.

    n = 1result = 6

  6. 6
    40
    11
    22
    13
    24
    i

    result

    4

    110 ⊕ 010 = 100

    2 again: XOR flips the same bits back, so the pair cancels out. 6 ⊕ 2 = 4.

    n = 2result = 4

  7. 7
    40
    11
    22
    13
    24

    result

    4

    binary 100

    Every pair cancelled to 0. Only the lonely number survives: return 4.

    result = 4

06Java solution

SingleNumber.java
public class SingleNumber {
    static int singleNumber(int[] nums) {
        int result = 0;
        for (int n : nums) result ^= n;
        return result;
    }

    public static void main(String[] args) {
        System.out.println(singleNumber(new int[] {2, 2, 1}));
        System.out.println(singleNumber(new int[] {4, 1, 2, 1, 2}));
        System.out.println(singleNumber(new int[] {-3, 7, 7}));
    }
}
Expected output
1
4
-3

The tricky line: result ^= n is short for result = result ^ n. ^ in Java is XOR, not "power".

07Why it works

XOR the whole array in any order: 4 ^ 1 ^ 2 ^ 1 ^ 2 = 4 ^ (1 ^ 1) ^ (2 ^ 2) = 4 ^ 0 ^ 0 = 4. Each pair becomes 0, and 0 changes nothing.

08Time and space

  • Time: O(n). One pass.
  • Space: O(1). One int.

09Edge cases

CaseResult
One elementThat element
Negative numbersWork: XOR acts on the bits
Single number is 0Returns 0 correctly

10Common mistakes

  • Thinking ^ means power. In Java, 2 ^ 3 is 1, not 8. Use Math.pow for powers.
  • Starting result at nums[0] and looping from 0. Then nums[0] is XOR-ed twice and disappears.

11Other approaches

ApproachTimeExtra space
HashSet add/removeO(n)O(n)
Sort, check pairsO(n log n)O(1)
XORO(n)O(1)

12In the interview

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. What is 5 ^ 5 in Java?

  2. 2. Why does the order of numbers not matter?

14Solve it yourself

Try it on LeetCode #136.