We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
All numbers except for 1 will be in pairs. xoring a value with the same number twice will reset all the flipped bits back to their original position. Therefore you start with 0 and xor every number that comes in. If a number occurs twice, the second xor will reflip the bits the first xor flipped. You will be left with the answer because it is the only number that flipped bits only once. It doesn't matter if other numbers flipped those bits in the mean time, as long as they came in pairs they will reset themselves. It's like simple XOR encryption.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lonely Integer
You are viewing a single comment's thread. Return to all comments →
All numbers except for 1 will be in pairs. xoring a value with the same number twice will reset all the flipped bits back to their original position. Therefore you start with 0 and xor every number that comes in. If a number occurs twice, the second xor will reflip the bits the first xor flipped. You will be left with the answer because it is the only number that flipped bits only once. It doesn't matter if other numbers flipped those bits in the mean time, as long as they came in pairs they will reset themselves. It's like simple XOR encryption.