Solving the problem: Find the odd int
Tags: javascript, beginners, functional
Published: 2019-10-13 19:39:31

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](https://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](https://thepracticaldev.s3.amazonaws.com/i/647hqwojl23cuam03vj9.png)

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](https://thepracticaldev.s3.amazonaws.com/i/meb4o4d1fnfn2jeiob8y.png)

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](https://thepracticaldev.s3.amazonaws.com/i/n015qvdmgm3w7ua0slm8.png)

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

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/esb612rs1tudy9kzoc5x.png)

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](https://thepracticaldev.s3.amazonaws.com/i/covtsyf0mbc4je3ocyaz.png)

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

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/41vg343oghp74u85byz3.png)

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](https://thepracticaldev.s3.amazonaws.com/i/bkdrkwsrf980t0yycl89.png)

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](https://www.codewars.com/kata/find-the-odd-int/), but you'll have to complete the problem first. Thanks for reading through!