Algorithm 101: 3 Ways to Check If Two Words are Anagrams
Tags: algorithms, javascript, anagram, string
Published: 2020-03-11 21:24:09

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 - [Wikipedia](https://en.wikipedia.org/wiki/Anagram).

Programmatically, can you do this? 

```javascript

wordAnagrams("silent", "listen"); // true

wordAnagrams("samson", "ebere"); // "unequal word lengths"

wordAnagrams("njoku", "ebere"); // false

```

We want to explore three (3) ways we can achieve this. We will be focusing on ``word anagrams`` only. In the future, we will look into [sentence anagrams](https://dev.to/ebereplenty/algorithm-101-3-ways-to-check-if-two-sentences-are-anagrams-55jc). The major objective is that both words should contain exactly the same amount of letters and these letters should re-occur the same amount of times in each word.

### Prerequisite
To benefit from this article, you need to have basic understanding of javascript's string, object and array methods.

### Let's do this using:

* sort(), length, toLowerCase(), if...statement, join()

```javascript
      function wordAnagrams(wordA, wordB) {
        let newWordA = wordA.toLowerCase();
        let newWordB = wordB.toLowerCase();

        if (newWordA.length === newWordB.length) {
          return (
            [...newWordA].sort().join("") === [...newWordB].sort().join("")
          );
        }

        return "unequal word lengths";
      }
```

* sort(), length, hasOwnProperty(), JSON.stringify(), toLowerCase(), if...statement, for...of...loop

```javascript
      function wordAnagrams(wordA, wordB) {
        let newWordA = wordA.toLowerCase();
        let newWordB = wordB.toLowerCase();

        function createWordObject(word) {
          let wordObject = {};
          for (char of [...word].sort()) {
            if (wordObject.hasOwnProperty(char)) {
              wordObject[char]++;
            } else {
              wordObject[char] = 1;
            }
          }
          return wordObject;
        }

        if (newWordA.length === newWordB.length) {
          wordAObject = createWordObject(newWordA);
          wordBObject = createWordObject(newWordB);

          return JSON.stringify(wordAObject) === JSON.stringify(wordBObject);
        }
        return "unequal word lengths";
      }
```

* sort(), length, hasOwnProperty(), toLowerCase(), Object.keys(), Object.values(), push(), .every(), if...statement, for...loop, for...of...loop

```javascript
      function wordAnagrams(wordA, wordB) {
        let newWordA = wordA.toLowerCase();
        let newWordB = wordB.toLowerCase();
        let result = [];

        function createWordObject(word) {
          let wordObject = {};
          for (char of [...word].sort()) {
            if (wordObject.hasOwnProperty(char)) {
              wordObject[char]++;
            } else {
              wordObject[char] = 1;
            }
          }
          return wordObject;
        }

        if (newWordA.length === newWordB.length) {
          let wordAObject = createWordObject(newWordA);
          let wordBObject = createWordObject(newWordB);

          let wordAObjectKeys = Object.keys(wordAObject);
          let wordBObjectKeys = Object.keys(wordBObject);

          let wordAObjectValues = Object.values(wordAObject);
          let wordBObjectValues = Object.values(wordBObject);

          let wordObjectLength = wordAObjectKeys.length;

          // check if wordA Object is equivalent to wordB Object
          for (let i = 0; i <= wordObjectLength; i++) {
            let check =
              wordAObjectKeys[i] === wordBObjectKeys[i] &&
              wordAObjectValues[i] === wordBObjectValues[i];

            if (check) {
              result.push(true);
            } else {
              result.push(false);
            }
          }

          return result.every(currentValue => currentValue === true);
        }

        return "unequal word lengths";
      }
```

### Conclusion

There are many ways to solve problems programmatically. I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

> Up Next: [Algorithm 101: 3 Ways to Create Pig Latin](https://dev.to/ebereplenty/algorithm-101-3-ways-to-create-pig-latin-513m)

You can also follow and message me on social media platforms. 

**[Twitter](https://twitter.com/eberetwit) | [LinkedIn](https://www.linkedin.com/in/samson-ebere-njoku-profile/) | [Github](https://github.com/EBEREGIT)**

Thank You For Your Time.