I am trying to get my hand dirty with Javascript
I want to solve the following exercise (found on Codewars):
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.
[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).
this is my code:
function findOdd(A) {
const isOdd = (x) => {
return x%2;
}
const counts ={};
for ( i of A)
{
counts[i]= counts[i] ? counts[i] +1:1;
}
const numOccurences = (Object.values(counts));
const occurences = Object.getOwnPropertyNames(counts);
let evenOccurence=0;
let oddOccurence;
for (let y=0;y<numOccurences.length;y++)
{
if(isOdd(numOccurences[y]))
{
console.log("numOccurences[y] is equal to: ",numOccurences[y])
evenOccurence = numOccurences[y];
console.log(`value ${occurences[y]} is appearing ${numOccurences} times which is even`)
}
// }
// console.log("evenOccurence",evenOccurence);
// return evenOccurence;
}
return evenOccurence;
}
// console.log(findOdd([7]));
console.log(findOdd([7,7,8,8,7,5,5,5,5,5,5,5]));
my issue is the value I am putting in evenOccurrence, I would expect an integer, but it comes as 7,3,2 in the array given as an example.
could anyone help me please?
Thanks in advance