Typorithm · easy

Single Number

▷ Interactive algorithm labsingle-numberbit-manipulationxorself-inversepairs-cancel
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Single Number

0%

Bit Manipulation

XOR every element; equal pairs cancel to zero, so the one unpaired value remains.

XOR is its own inverse: x XOR x = 0, and x XOR 0 = x.

Folding the whole array with XOR cancels every value that appears twice.

Only the element appearing once is left, found in one pass with O(1) memory.

Preparing algorithm lab…
01

x XOR x = 0, x XOR 0 = x.

02

Pairs cancel out.

03

One pass, O(1) space.

function singleNumber(nums) {
  let result = 0;

  for (const num of nums) {
    result ^= num;
  }

  return result;
}

Read the implementation

Connect each part of the code to the concept before you type.

1. XOR identity

Zero is the identity: x XOR 0 = x.

let result = 0

2. Fold with XOR

XOR every value; pairs cancel to zero, leaving the unique one.

result ^= num

3. The loner

After all cancellations, only the single number survives.

return result

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Single Number: Visual Walkthrough & Practice · Typorithm | ApplyDjinn