Check Whether Two Strings Are Anagrams Without JavaScript’s .sort() Method

Sebastien Dornel
The Startup
Published in
5 min readSep 22, 2020

This week I’ll cover one way of creating an algorithm to calculate whether two strings are anagrams.

In this algorithm, you are given two strings and are required to find whether or not they are anagrams of each other.

An anagram is a word or phrase that is formed by rearranging the letters of another word. An example is “cinema” which can be formed using the letters of “iceman”. Another example is “listen” which can form “silent”. Note that these words have the exact same number of letters as their counterparts. If a word has an extra letter (take “reduce” and “reducer” for instance), then it will not be considered an anagram even though the letters of reducer are capable of forming the word reduce.

If you need further clarification, a quick google search will be able to give you all the information you require.

With this algorithm, what we are trying to achieve is this:

Example 1:   Input: “iceman”, “cinema”   Output: trueExample 2:   Input: “abcd”, “fghi”   Output: falseExample 3:   Input: “reduce”, “reducer”   Output: false

For the sake of simplicity, we will assume that all inputs are strings that possess no uppercase letters. In addition, you are not allowed to use the .sort() method.

So, to begin with we have an empty function:

function isAnagram(a, b){}

Before moving forward, I am of the opinion that it is vital to create an action plan detailing what you need to achieve in order to achieve your result.

With that being said, what we essentially need to do is compare two strings to check if they each have both the same characters as well as the same number of characters WITHOUT using the .sort() method.

In order to compare the two strings without using .sort() we need to either re-create the .sort() method or find another way to manipulate the data such that we are able to compare the information from both strings.

I have chosen to proceed by manipulating the data without creating my own sorting method.

At this point, you may be wondering how it is possible to modify a string without using .sort(). I will be doing so by creating two objects with keys being letters and values being the frequency that these letters are found within a string.

For example:

“apple” = {“a”: 1, “p”: 2”, “l”: 1, “e”: 1}

Going back to the empty function I wrote earlier, we can now move forward with the process of turning our strings into objects.

You could now say that we have reworded this problem into something that makes more sense from a coding perspective. In addition to checking to see if two strings are anagrams, we are also comparing two objects and checking for equivalency.

Inside that function, we can also potentially increase its speed (provided the small increase in memory is not a concern) by first checking to see if both strings are the same length. If not, return false.

Running this function by itself should grant the following output:

createObject(“iceman”, “battery”)[ { i: 1, c: 1, e: 1, m: 1, a: 1, n: 1 }, { b: 1, a: 1, t: 2, e: 1, r: 1, y: 1 } ]

Now that we have our objects as well as validation to check if both strings are the same length, we can create a function which compares them and checks for equivalence.

function isAnagram(a, b){}

We should also refactor the code of the first function so that it calls the isAnagram function at the end rather than returning an array of objects.

Going back to the isAnagram function, we now need to figure out how to compare both objects. For those of you newer to programming, you might think that all we need to do is:

return s1Obj === s2Obj // if equal, will return true. Else returns false

Unfortunately, this is not possible as two objects or arrays (even if their contents are the same) will never be equal to one another.

We can now check to see if both objects contain the same characters. There is more than one way to do this. I will merely show you one method of comparing object keys. After that it is only a simple matter of comparing the lengths of both resulting arrays to see if they possess the same letters.

If you have been coding along, you will now be able to check if strings are of equal length and find out if they have the same letters.

Next we will check if strings of equal lengths possess the same combination of characters. A for loop will be utilized for this.

As you can see above, we are able to access object values by entering in their keys. If a certain key-value pair is not equal to its counterpart, then the function returns false. If none of the conditional statements in either functions are triggered, then it is assumed that the strings are anagrams.

createObject(“apple”, “mapple”) // falsecreateObject(“apple”, “maple”) // falsecreateObject(“apple”, “paple”) // truecreateObject(“apple”, “apple”) // truecreateObject(“iceman”, “cinema”) // truecreateObject(“through”, “driveway”) // falsecreateObject(“misc”, “derc”) // falsecreateObject(“miscellaneous”, “misc”) // falsecreateObject(“thru”, “urth”) // truecreateObject(“reduce”, “reducr”) // false

And there you have it! I just guided you through a way to create an algorithm that checks if two strings are anagrams of one other.

Here is the GitHub repository for this algorithm:

https://github.com/sdornel/algorithms

--

--