This week, I decided to create a HackerRank account and begin to work through the Interview Preparation Kit. This coding challenge’s instructions state that your function will receive a passed in array of numbers that correspond with colors. You need to take the passed in array and return the number of pairs in the entire array. I know there are a few ways to find a solution to this problem. The first that comes to mind is creating a nested loop to check if each element in the inner loop is the same as the current element from the outer loop. This isn’t ideal, since creating a nested loop would give the function a time notation of O(n²). So, I’ll have to find a more efficient way to complete the function.

To complete this challenge, I want to create an empty object to push the elements of the array into (I’ll go further into why this is necessary later on) and a variable set to zero. I’ll add one to this variable whenever there’s a matching pair and return it at the end of the function.

Next, I’ll have to fill that empty object with the information passed to the function as an array. For this, I decided to use JavaScript’s .forEach method. This method will take each item and turn it into a key value pair in the obj object.

Inside the brackets, I want this method to take each item and start the process by checking if a key of that item already exists in the object. If the key exists, I want the method to increment the value of the existing key by 1, but if it doesn’t, it should create a new key with the value of 1. This could be done using an if…else statement, but it could also be done in a single line using a ternary operator.

Once my object is populated with information, I need to take that information and add all pairs into the solution variable. I decided to use a for loop, setting the constant ‘property’ for each key value pair in the object.

Inside this loop, since the instructions are to look for pairs of socks, I knew I’d have to divide each value in the object by 2 to account for sock pairs. Since dividing by 2 would leave an extra half whenever there isn’t a matching pair of a certain value, I could remove those unmatched pairs by using the Math object’s floor method to get rid of that decimal value. This strategy would turn each value in the object into the correct number to be passed into the solution variable at the end of each iteration in the loop. After the loop finishes and the solution variable is filled, all I’d have to do is return that solution.

This solution passes all tests! There are a few different ways to have found this solution, but I believe this is one of the most efficient solutions. Since there aren’t any nested loops in this function, the time complexity should be O(n), which is an ideal outcome.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store