Single Number

easy

Find the element that appears only once when all others appear twice using XOR

Single Number

Key Insight

XOR all elements. Pairs cancel (a^a=0), single remains (a^0=a).

Step 1XOR Property
XOR
5
5
=
0
0
0
0
0
1
0
1
^
5
5
=
0
0
0
0
0
1
0
1
result
0
=
0
0
0
0
0
0
0
0
76543210
5 ^ 5 = 0

a ^ a = 0 (same numbers cancel)

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Single Number

XOR all elements. Pairs cancel (a^a=0), single remains (a^0=a).

  1. XOR Property

    a ^ a = 0 (same numbers cancel)

  2. Identity Property

    a ^ 0 = a (XOR with 0 keeps the number)

  3. Apply to Array

    Array [2, 3, 2]. XOR all: 2 ^ 3 ^ 2

  4. Pairs Cancel

    (2 ^ 2) ^ 3 = 0 ^ 3 = 3

  5. Result

    The single number is 3!