Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 08 · DSA · Worked problems, in English · about 16 min to read

Play it as a journeyTutor comes with a plan

Clinic #32: Single Number

Thinking in Bits
Thinking in Bits · Open board

The five moves a processor can make on a number’s row of switches. This clinic uses one of them, XOR, the move that flips a switch only where two rows disagree, and shows why it is the whole answer to one of the most asked easy problems.

Before you start. A computer stores every whole number as a row of ones and zeros, a row of switches read in twos instead of tens, so 101 is one four and one one, five. Bit Manipulation (thinking in bits) shows that row and the moves on it. This clinic needs only the row itself, and builds the one move it uses from scratch.

Lesson 71 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • The laundry basket: one sock has lost its partner
  • By hand: five socks, and your eye does it instantly
  • The wrong first idea: hunt for each sock’s twin
  • The table: faster, and it breaks the rule
  • The panel: sixteen switches and XOR
  • Why pairs vanish: XOR undoes itself
  • The invariant: the panel is what is still unpaired
  • 1988: the same trick keeps a disk alive
  • Brute force: count every value · O(n²) time, O(1) space
  • Better, twice: sort, or remember · O(n log n) or O(n) space
  • Best: XOR everything · O(n) time, O(1) space
  • Two wrong versions beside the four right ones
  • Timings
  • In an interview
  • The XOR ladder: easy, medium, hard

Terms it explains: O(n²), linear time, O(n), Constant extra space, disagree, XOR, invariant, Write the four lines,, Missing Number, Single Number III.

Review DSA