Finding Odd int problem!

Hello all,

I’m doing a problem on Codewars (Javascript) and I’ve very close to the solution ( i think) but I’m unsure of what to do next in regards to returning the that appears an odd number of times.
I chose to put the array in an object and keep the tally in the object, then take the number from there ( I wanted to work with an object. I know I probably could have just used the array).

EDIT: Thanks for the follow up. I worded my issue differently, and I actually found the solution on my own. I will be deleting the code I put here, don’t want to give anyone the answer.
Problem 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.

my code so far


Thank you for any and all help. Much appreciated.

I don’t understand how your question relates to the problem description. You’re not supposed to find the number that appears the most times, you’re supposed to find the (ONE!) number that appears an odd number of times.

1 Like
      //populate object with every number in array
      nums[value] = counter;

But aren’t your arrays nested? I don’t know - your formatting is a little idiosyncratic.

So, you’re looping each number, initializing your object, then running through the entire array to fill that object? You do the inner loop each time?

What if you did just one pass? You check each one - if it hasn’t been initialized, you initialize it. If it has, then you increment it. That would be an O(n) solution, as opposed to the O(n^2) you’re trying to do.

And I don’t think your analysis of the data is right, with your reduce. Why are you comparing size. You should be checking if it’s odd or not. I wouldn’t use reduce for that - that will run through the whole array. You only need to go until you find the first odd number.

1 Like