Coding: Check if Two Strings are Anagrams in Javascript

Monu Kumar Modi
The Fresh Writes
Published in
4 min readJan 24, 2023

Introduction: An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, “act” and “cat” are anagrams of each other. An anagram does not have to have the same meaning as the original word or phrase.

Solution1 :

Checking if two strings are anagrams of each other or not.

In this article, we will be discussing an efficient method for anagram detection using JavaScript.

The first step in our function is to remove any spaces and convert both strings to lowercase letters. This is done by using the replace method to remove all spaces in the string and the toLowerCase method to convert all characters to lowercase.

str1 = str1.replace(/\s/g, '').toLowerCase()
str2 = str2.replace(/\s/g, '').toLowerCase()

The next step is to check if the length of both strings is equal or not. If the lengths are not equal, we can return false as the two strings cannot be anagrams of each other.

if (str1.length !== str2.length) {
return false
}

Once we have confirmed that the lengths of both strings are equal, we can move on to sorting the characters in both strings.

str1 = str1.split('').sort().join('')
str2 = str2.split('').sort().join('')

The last step is to compare the sorted strings and see if they are equal or not. If the sorted strings are equal, then the original strings are anagrams of each other.

return str1 === str2

Complete Code :

function isAnagram(str1, str2) {
// Removes spaces and convert into the lowercase letter

str1 = str1.replace(/\s/g, '').toLowerCase()
str2 = str2.replace(/\s/g, '').toLowerCase()
// console.log(str1, str2)

if (str1.length !== str2.length) {
return false
}

// Sort the characters in the given string
str1 = str1.split('').sort().join('')
str2 = str2.split('').sort().join('')

// Compare the sorted String

return str1 === str2
}

const check = isAnagram('modi', 'monu')
console.log(check) // False

To test the function, we can call it and pass in two strings as arguments.

const check = isAnagram('modi', 'monu')
console.log(check)

The function will return false as ‘modi’ and ‘monu’ are not anagrams of each other.

This is a simple and efficient way of checking for anagrams using JavaScript. By removing spaces and converting the strings to lowercase letters, comparing the lengths, and sorting the characters in both strings, we are able to check if the two strings are anagrams of each other or not.

Solution 2:

Checking if two strings are anagrams of each other or not.

The first step in our function is to check if the length of both strings is equal or not. If the lengths are not equal, we can return false as the two strings cannot be anagrams of each other.

Next, we remove spaces and convert both strings to lowercase letters, so that we can compare the letters regardless of case and spaces.

We create a counter object to keep track of the number of occurrences of each letter in str1. We iterate through str1 and for each letter, we increment the count of that letter in the counter object.

Next, we iterate through str2 and check if the letter exists in the counter object. If it does not, we return false as the two strings cannot be anagrams.

Finally, we return true, if all letters in str2 also exist in str1 and the count of each letter is equal in both strings.

Complete Code:

function isAnagram(str1, str2) {
if (str1.length !== str2.length) {
return false
}
str1 = str1.replace(/\s/g, '').toLowerCase()
str2 = str2.replace(/\s/g, '').toLowerCase()
let counter = {}
for (let letters of str1) {
counter[letters] = counter[letters] ? counter[letters] + 1 : 1
}
for (let items of str2) {
if (!counter[items]) {
return false
}
counter[items] -= 1
}
return true
}
let str1 = 'I am Monu'
let str2 = 'am I OnMU'
const checkAnagram = isAnagram(str1, str2)
console.log(checkAnagram)

To test the function, we can call it and pass in two strings as arguments.

let str1 = 'I am Monu'
let str2 = 'am I OnMU'
const checkAnagram = isAnagram(str1, str2)
console.log(checkAnagram)

The function will return true as ‘I am Monu’ and ‘am I OnMU’ are anagrams of each other.

This is an efficient way of checking for anagrams using javascript, by first comparing the lengths of the two strings and then using a counter object to keep track of the number of occurrences of each letter in str1, and then comparing it with the letters in str2 we are able to check if the two strings are anagrams of each other or not.

If you’re interested in learning more about coding in Javascript, be sure to follow me for more code snippets and examples. I’ll be sharing valuable information and tips on how to master the language and improve your skills as a developer. So, stay tuned for more updates, and let’s continue learning together.

Thanks for reading.Happy learning 😄

Do support our publication by following it

--

--