# JavaScript Coding Challenge #2

We’re going to take a look at another fun challenge to solve in JavaScript. For this one we’ll need a bit more knowledge than for our first challenge: **Multiple of 3 or 5**, as it will involve slightly more elements.

The challenge is from HackerRank, a great place where you can solve multiple challenges in many programming languages. It’s a great way to test your skills and it’s also used by companies in their recruiting process. There are also different coding contests with prizes.

Ok… back to our challenge. Let’s find out what is the description of it:

*Note**: I adjusted slightly the challenge description from HackerRank as we aren’t using a server to get the file which has the inputs, but rather we’ll pass the input as the function arguments (makes our life easier :D). But you could definitely tweak it just a little bit to work well if you want to submit it to HackerRank.*

# Sock Merchant

John’s clothing store has a pile of loose socks where each sock

`i`

has a value denoting its color`socks[i]`

. He wants to sell as many socks as possible, but his customers will only buy them in matching pairs. Two socks are a single matching pairs if their value are equal:`socks[i] === socks[j]`

.Given an array

`socks`

which will hold all the socks value, how many pairs of socks can John sell?

Input FormatWe’re given an array

`socks`

where each item in the array`socks[i]`

contains a value representing the sock color.

Sample inputAn array of color values representing each sock’s color:

`socks = [10, 20, 20, 10, 10, 30, 50, 10, 20];`

Sample output

`3`

Explanation

OK. Now that we have the problem’s description, we can get into some coding! Let’s create a function called `socksForSale`

which will get as a parameter the `socks`

array:

function socksForSale(socks){}

We need to consider the case where the `socks`

array isn’t sorted, that means we need to sort it. This will help us later.

JavaScript arrays have a built in function called `sort`

which comes in handy:

function socksForSale(socks){ socks.sort();}

But there’s a catch with this `sort`

function when it comes to numbers. For example if you have the following unsorted array: `[4, 32, 1, 10, 20, 99, 14, 42]`

and you’re applying the `sort`

function on it, the array becomes: `[1, 10, 14, 20, 32, 4, 42, 99]`

.

It’s a little strange, but this is how the `sort`

function works. We’re going to create our own sorting function that takes as it’s own parameters 2 numbers `a`

and `b`

and return their difference. Then we’ll use this function as a parameter for the array’s built in `sort`

function. This will ensure we’ll get the desired result, as in our example: `[1, 4, 10, 14, 20, 32, 42, 99]`

.

Let’s add this to our code:

function socksForSale(socks){ socks.sort(function(a, b){

return a - b;

});}

Awesome! We now have a sorted array. What’s left to do is to loop through every item in the array and increase a counter each time 2 adjacent numbers are equal. This way will get the number of matching socks and we can return it. Pretty easy right?

function socksForSale(socks){ socks.sort(function(a, b){

return a - b;

}); var counter = 0; for(var i=0; i<socks.length; i++){

if(socks[i] === socks[i+1]){

counter++;

}

} return counter;

}

We’re done! Congratulations!

Oh… wait… something is wrong… If we have an array like: `[10, 10, 10]`

the counter will become `2`

. *Why?* That’s because we are comparing 2 times the value from index `1`

(the middle one) with both values at index `0`

and `2`

. No, no… that’s not ok, we can’t have 2 pairs of socks if we only have 3 socks, right?

Hmmm… *How can we avoid this little issue???*

What if we skip the next element if there is a match by increasing the value of `i`

? Let’s try that:

function socksForSale(socks){ socks.sort(function(a, b){

return a - b;

}); var counter = 0; for(var i=0; i<socks.length; i++){

if(socks[i] === socks[i+1]){

counter++; i++;

}

} return counter;

}

That works! Now we’re getting the right number of pairs of matching socks each time!

There is just a small modification I want to make. As we can see now the loop is going from index `0`

all the way to index `socks.length-1`

as we have `i<socks.length`

. In some programming languages when we get to the last index of the array: `socks[socks.length — 1]`

and we compare it’s value with the next item’s value which is: `socks[socks.length`

we get: `undefined`

when we work in JavaScript; but in other programming languages we can get into some trouble as that index of the array is absent. That means that we only need to loop up to: `i<socks.length — 1`

:

function socksForSale(socks){ socks.sort(function(a, b){

return a - b;

}); var counter = 0; for(var i=0; i<socks.length - 1; i++){

if(socks[i] === socks[i+1]){

counter++; i++;

}

} return counter;

}

Let’s test the code:

var socks = [1, 30, 2, 10, 8, 2, 10, 8, 4, 2, 1];

var socks2 = [10, 20, 20, 10, 10, 30, 50, 10, 20];socksForSale(socks); // returns 4

socksForSale(socks2); // returns 3

Perfect! Congratulations!

# Conclusion

I hope you enjoyed this coding challenge. It was pretty simple and I’m pretty sure that you can come up with another solution for it which is much more elegant, this is why I’ll encourage you to do so and then post your solution in the comments section bellow! :D

Take it a step further and see what if we would also have another array passed to the function representing each sock’s brand name? This way not only you have to see if they have matching colors, but also if they have the same brand name. This would be very interesting!

If you liked my post, I would sincerely appreciate a click on the **Recommend button**. 💚