4 ms·
This only works if the numbers are in a known range (say sequential from 1 to 100), and you XOR in the index (plus 1) as well. Then each number is XOR'd two tim
by ammon 11y ago
This only works if the numbers are in a known range (say sequential from 1 to 100), and you XOR in the index (plus 1) as well. Then each number is XOR'd two times, except the duplicate, which is XOR'd 3 times (and thus remains at the end). The fact that the code is wrong shows why this question is a very bad interview question.
EDIT
The given code works to find the only non-duplicate item in a list (perhaps that was what was intended)
- eranation 11y agoyes,that was the intention, I can't edit by now. I had to leave fast and then got "noprocast" on and couldn't reply / correct but it is nice that people got what I said through the lines! it should have been called findNonDupe and the var should have been called nonDup. Hope I'll do better in a real interview ;)