The id with no pair
transcript
Every message id is logged twice. One id was logged once. Which one is it? Do not count them. Cancel them. Exclusive or does the cancelling.
Take nine, in eight bits. Put nine beside it. Exclusive or is one only where bits differ. No column differs. So a number exclusive or itself is zero.
And a number exclusive or zero stays the same. Order does not matter. So pairs cancel anywhere in the list.
The ids are four, nine, four, seven, nine. Keep a running exclusive or. Four, then thirteen, then nine. Then fourteen, and then seven. Seven has no pair. It is the answer.
One pass over the ids. One number of memory.
A map of counts also finds the lone id. But it needs memory for every distinct id. Exclusive or keeps one number. It needs every other id to come in pairs.
Negative ids work too, because exclusive or works on bits. In JavaScript, bitwise operators cut numbers to thirty two bits. Ids above about two point one billion need BigInt there.
Same rule, any input. Full breakdown in the lecture.