Write anagram check function with array and string methods

Share this video with your friends

Social Share Links

Send Tweet

In this lesson you will learn how to write an anagram check function with 'array.filter' and 'array.sort' methods. We will use functional programming approach to compare if two string have the same letters but in different order, excluding all special characters like spaces, exclamation marks etc.

Dimitri Ivashchuk: [0:00] You've got asked at the interviewer to perform an anagram check or write an anagram check function. To start, what is anagram? Anagram is the word which contains all the letters of another word. In this case, astronomer and monster, if you remove the space here, those are formed by exactly same letters used only once.

[0:25] We want to write a function which checks that, const anagramCheck. We want to get two arguments. It would be stringOne and stringTwo. Then we want to return true or false if the strings are anagrams or not. Let's first think of how we approach the question.

[0:58] To check the strings for the equality, we first need to lowercase them in case the strings, let's say, look something like this. We want to mitigate the issue of the case first. Then we want to arrange the letters in the string by alphabet. The strings, if they're anagrams, have the exact same structure. You will see what I mean in a second.

[1:31] Let's write a utility function so we don't have to repeat our logic for modifying both strings, so const modifyString, accepts a string. For now, we want to just lowercase the string with a native string lowercase method, toLowerCase(). Let's look what we have for both strings. You see astronomer moon starer. If we now change the case, then nothing happens. We can continue.

[2:10] What we want to do next is split the whole word so we get the array of letters. You see that now we get the array of each individual letter coming from the anagramOne and anagramTwo. Now we want to order them. We use the sort array method. You see that we have the letters ordered by alphabet now.

[2:38] The bad thing is that we have this space in moon starer, and we want to remove that. We want to also filter, then we do the char, then we do the char.match. What this does, it removes all the special characters from the string. If I do something like +++, lots of spaces, you see that it doesn't affect our strings as we log them with modifyString function.

[3:21] We can confirm that by putting the exclamation mark there. We want to filter all the normal characters now and to return only the ones which are special. You see that from this string we got only pluses, and from this string we got the spaces and three exclamation marks. Let's remove those and put our strings and functions back to the normal. We want to join.

[3:54] As you see, we have two strings with modified letters, and we order them in the same way. What we can do now is to just return modifyString(stringOne) === modifyString(stringTwo). Let's console.log this. You see that it's true. Returned value is true. Let's try to pass notAnagram here. You see that it's false.

[4:40] This function performs an anagram check by doing some native string operations, native array methods, and comparing then two modified strings to one another.