Easy peasy First Odd Int
In a given array, find the first integer that appears an odd number of times. Given that only one integer occurs odd number of times.
const findOddInt = (arr) => arr.reduce((a, b) => a ^ b);
Always go for functional and tuned solution, reason it being faster ;)
For those who are wondering, ^ is the symbol for XOR.
a^a = 0 and
0^a = a. So, all the numbers that occur even times will get reduced to 0 and the number that occurs odd number of times would remain.
3.4K+ developers have started their personal blogs on Hashnode in the last one month.
Write in Markdown · Publish articles on custom domain · Gain readership on day zero · Automatic GitHub backup and more