Cracking the Code Challenge: Fun with Anagrams

thequeenbeebs
4 min readJul 29, 2021
Photo by AltumCode on Unsplash

During my job search process for junior software engineering positions, I frequently am asked to tackle code challenges to demonstrate my knowledge and ability to potential employers. My jaw just about dropped to the floor when I realized I was encountering the exact same HackerRank problem twice in one week. I wasn’t able to solve it in time during the first attempt, but was very pleased to pass all of the tests the second time around.

Since I am imagining I will encounter this problem again, I figured I would break down my solution and try and help other junior developers.

Note: I’m sure this is not the most efficient solution, this is just what I logically came up with in a limited amount of time!

THE PROBLEM

Given an array of strings, remove each string that is an anagram of an earlier string, then return the remaining array in sorted order.

Example

str = ['code', 'doce', 'ecod', 'framer', 'frame']

code and doce are anagrams. Remove doce from the array and keep the first occurrence code in the array.

code and ecod are anagrams. Remove ecod from the array and keep the first occurrence code in the array.

code and framer are not anagrams. Keep both strings in the array.

framer and frame are not anagrams due to the extra r in framer. Keep both strings in the array.

Order the remaining strings in ascending order: ['code','frame','framer'].

MY SOLUTION

function funWithAnagrams(array) {   let sortedArray = array.map(string =>       
string.split('').sort().join(''))
let firstAnagram = [sortedArray[0]] let indices = [0] let results = [] for (let i = 1; i < sortedArray.length; i++) { if (firstAnagram.includes(sortedArray[i])) { } else { firstAnagram.push(sortedArray[i]) indices.push(i) } } indices.forEach(index => { results.push(array[index]) }) return results.sort()}

THE METHOD TO MY MADNESS

Now, I’m going to attempt to break down my code line by line and explain how this problem works.

sortedArray

The first thing I did was create sortedArray, where I took the input array, split it by characters, sorted those characters in alphabetical order, and then joined them back together. If you used the example above, sortedArray would look a little like this:

['cdeo', 'cdeo', 'cdeo', 'aefmrr', 'aefmr']

Now, I can clearly see which strings are anagrams of each other.

firstAnagram and indeces

These are both arrays that we are going to push items into when we are looping through our sorted array; indeces will contain the indeces of each string where an anagram is seen for the first time, and firstAnagram will contain each anagram we come across. For now, we put 0 in the indeces and the instance of sortedArray[0] into firstAnagram, as we need something to compare to in our loop, and this function will always return the first item in the array.

Looping Through sortedArray

This is a pretty basic for loop, with the exception that we are starting with i = 1, because the string at index 0 is already included.

If/Else Conditional

Now, this logic is a little out there but try to follow me on this one. Using the array.includes() method, I check and see if the firstAnagram array contains the string found at sortedArray[i].

If the firstAnagram array already includes that string, I don’t want to do anything: we already have an earlier string that is an anagram, so that one will be omitted.

However, if firstAnagram does not include that string, we should add it to the firstAnagram array, and also add the index of that string to the indeces array. I do both of these in the else statement using the array.push() method.

Looping Through indeces

Now, we will have an array, indeces, that contains the index of each first instance of an anagram! In the above example, it would look like this:

[0, 3, 4]

Now, we need to take the strings at the corresponding indeces from the original input array. I created an empty array at the top of the function called results. So, using a forEach loop, we will loop through the indeces array, and push the corresponding string from the input array onto our results array.

Sorting

Last but not least, the problem asked us to return the array in sorted order. Because this is simply strings, always we have to do is return results.sort().

So, there we have it! Let me know if any of this is still confusing and I will try to break it down for you. Happy coding!

--

--

thequeenbeebs

Blaire is a musical theatre performer who also moonlights as a full-stack software engineer. https://www.linkedin.com/in/blaire-baker