Coding Challenge Practice - Question 77
Source: Dev.to
Problem Description
Implement a function that finds the integer that appears only once in an array where every other integer appears exactly twice.
Solution Explanation
The XOR operation (^) has a useful property:
a ^ a = 0– two identical numbers cancel each other out.0 ^ b = b– XOR with zero leaves the other number unchanged.
By XOR‑ing all elements of the array, every pair of identical numbers reduces to 0, leaving only the single, unpaired integer.
Code
function findSingle(arr) {
let result = 0;
for (let num of arr) {
result ^= num;
}
return result;
}