So here is the riddle:
There is a list of numbers, where each number appears in the list twice. Except there is one number that appears once. I give you the list one number at a time.
The question is: can you find out what the number is that appears only once?
You can’t save the whole list and then look at it later. You can only store at most one number. In jargon, you must do it in constant space.
For example if I gave you the list 1,3,5,3,8,5,1, then 8 is the number that only appears once.
UPDATE:
So the condensed version of riddle is:
Given a list of numbers read off one by one where each number appears twice, and one number that appears once, can you find the number that appears once by only storing one intermediate number?
I love this kind of thing.
If the numbers are a list of integers.
Then, using the bitwise XOR (exclusive or) operator iteratively
and a variable ‘store’
do while{
store = store ^ nextValue /* Where ‘^’ is the bitwise XOR operator
} there are still numbers in the list
All the similar values will XOR to zero and the odd man out will be left in ‘store.’