loading...
Cover image for Solving the problem: Find the odd int

Solving the problem: Find the odd int

jadejdoucet profile image Jade Doucet ・4 min read

In this post, I will be going over my solution for the problem "Find the odd int", which could be a potential interview question!

So, given an array of integers, find the integer that appears an odd number of times.

Sounds simple enough, right? Maybe not so much if you're a beginner developer. However, you'll feel pretty confident with this one after this walk through. If you'd like to try this one out before actually seeing my solution to the problem, feel free to open up a fresh repl.it.

First things first, I'll start with some pseudo-code. We have an array of integers that looks something like this.

Alt Text

There is only one integer here that appears an odd number of times. Don't worry about trying to count and figure it out in your head, we can make a function for that!

We know that we need a way of keeping track of each item in the array. Then, we need to keep a count of how many times each item has appeared within an array. We don't want to manually assign variables to each number or some simple shortcut like that, we want our function to work with even the largest of arrays. An easy way to keep track of an item, and how many times it appears may be done in an object. Also keeping in mind that we will need to return the single integer that appears the most, we can make a variable for that too.

Alt Text

So, we have our object to keep track of items from the array, then we have our result. Now our next step will be to loop over the array. We want only one of each value inside of our object, so we have to keep that in mind as well, probably as a condition. In code, we can use a forEach to loop over the array, and then simply create a condition at each item. If the item exists as a key on our counterObject, then we will simply increment the number at that object index by 1. If the item does not exist in our counterObject, we will just create the key and set it at a base number of 1, representing this numbers first occurrence within our array. In code, that will look something like this:

Alt Text

Now, if I throw a console log in this function after the forEach function, we will see an ugly object like this

Alt Text

Cool, so now we can see that our object has a bunch of keys representative of every number that we've encountered in our array. Each item also has a corresponding number for the amount of times that it has appeared within our array. Awesome! Now we have exactly what we wanted.. but not quite. We still have to return that number which appeared an odd amount of times. We can see that the number, 7, has appeared 3 times, and the rest of the integers are even. We are on the right track, but now we need to think of a condition, as well as to loop over our object. We can loop with a simple 'for-in' loop, but we need to check each number to be odd. An easy way to do this, is by using the remainder operator, '%' (also referred to as modulo)! I know that a lot of people that aren't very math savvy are intimidated by this bugger, but don't fret. Our implementation with it is very simple. Since the remainder operator basically returns the number left over after an item has been divided as many times as possible by the number on the right without returning a decimal.

Keeping this all in mind, to find out if these values are even or odd, we can simply get the remainder after each number has been divided by 2 over and over. If a number is even, it's divisible by 2, so the result will be 0. However, we want that outlying odd number, which will return a remainder of 1 instead.Using that condition, we can just assign our result variable that we created earlier to the only number that passes this condition.

Lets implement that in our code!

Alt Text

Sweet! So we are done, right? We returned our result, and it's the odd number.
Wait...

Alt Text

Ughh, even more steps! We are returning a string because our object converted the number to a string! Don't worry. We can just add one more bit of code to fix this.

Alt Text

Boom, number. This is the most legible solution for myself, but there are many implementations of this problem. Feel free to check out other solutions on codewars, but you'll have to complete the problem first. Thanks for reading through!

Posted on by:

jadejdoucet profile

Jade Doucet

@jadejdoucet

Full-Stack Developer | Software Engineer | Web Developer

Discussion

pic
Editor guide
 

Well written.

Small tidbit, you can break or simply return num inside the for loop on line 39. No need to keep checking once you got one.

 

Ah, thank you! I will take that into consideration next time.