// Given an array of integers, find the one that appears an odd number of times.
// There will always be only one integer that appears an odd number of times.
// Examples
// [7] should return 7, because it occurs 1 time (which is odd).
// [0] should return 0, because it occurs 1 time (which is odd).
// [1,1,2] should return 2, because it occurs 1 time (which is odd).
// [0,1,0,1,0] should return 0, because it occurs 3 times (which is odd).
// [1,2,2,3,3,3,4,3,3,3,2,2,1] should return 4, because it appears 1 time (which is odd).
// solution
function findOdd(A) {
const counts = {};
A.forEach(item => {
counts[item] = (counts[item] || 0) + 1;
});
for (const key in counts) {
if (counts[key] % 2 !== 0) {
return Number(key);
}
}
}
function findOdd(A) {
return A.reduce((acc, num) => acc ^ num, 0);
}
To embed this project on your website, copy the following code and paste it into your website's HTML: