Home > katas > Find the odd int (6kyu) [JavaScript]
Find the odd int (6kyu) [JavaScript]
Chek kata on Codewars
Description:
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.
HashTable
function findOdd(arr) {
var hashTable = {};
arr.forEach(function(el){
hashTable[el] ? hashTable[el]++ : hashTable[el] = 1;
});
for(let prop in hashTable) {2
if(hashTable[prop] % 2 !== 0) return Number(prop);
}
}
Find + Filter
function findOdd(arr) {
return arr.find((item) => arr.filter(el => el == item).length % 2)
}
XOR
function findOdd(arr) {
return arr.reduce((a, b) => a ^ b)
}