## DEV Community is a community of 660,291 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Algorithm 202 (My Interview Question): Grouping Anagrams in 3 Ways NJOKU SAMSON EBERE
I love providing solutions to real world problems. When I am not coding, I enjoy movies, hanging out and working out.

Last year, I had a `technical interview` and one of the questions was on `Anagrams`. I solved the problem in 3 ways today and I want to share it with you in a moment.

```Question:

Given an array of strings, group anagrams together.

Anagram: These are words that are made up of the same letters but in different orders.

Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]

Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]

```

If you have been following my Algorithm series, then you are well fortified for this challenge.

``````
groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]);

/*
[ [ 'ate', 'eat', 'tea' ], [ 'nat', 'tan' ], [ 'bat' ] ]
*/
``````

### Solution

• .map(), sort(), join(), Set(), forEach(), filter(), push, spread operator
``````      function groupAnagrams(array) {
let finalArray = [];

// rearrange each word to check for anagram
let rearranged = array.map(element => {
return [...element].sort().join("");
});

// remove duplicates
let uniqueArray = [...new Set(rearranged)];

// compare original array with dupliates
uniqueArray.forEach(word => {
let chunk = array.filter(char => word === [...char].sort().join(""));

finalArray.push(chunk.sort());
});

return finalArray;
}
``````
• for...of...loop, sort(), join(), filter(), push, split(), indexOf()
``````      function groupAnagrams(array) {
let finalArray = [];
let rearranged = [];

// rearrange each word to check for anagram
for (element of array) {
rearranged.push(
element
.split("")
.sort()
.join("")
);
}

// remove duplicates
let uniqueArray = rearranged.filter(
(member, index) => rearranged.indexOf(member) === index
);

// compare original array with dupliates
for (word of uniqueArray) {
let chunk = [];

for (char of array) {
if (
word ===
char
.split("")
.sort()
.join("")
) {
chunk.push(char);
}
}
finalArray.push(chunk.sort());
}
return finalArray;
}
``````
• for...loop, while...loop, sort(), join(), push(), split(), includes()
``````      function groupAnagrams(array) {
let finalArray = [];
let rearranged = [];

// rearrange each word to check for anagram
let i = 0;
while (i < array.length) {
rearranged.push(
array[i]
.split("")
.sort()
.join("")
);
i++;
}

// remove duplicates
let uniqueArray = [];
for (let j = 0; j <= rearranged.length; j++) {
if (!uniqueArray.includes(rearranged[j])) {
uniqueArray.push(rearranged[j]);
}
}

// compare original array with dupliates
let counter = 0;
while (counter < uniqueArray.length) {
let chunk = [];

for (let k = 0; k < array.length; k++) {
if (
uniqueArray[counter] ===
array[k]
.split("")
.sort()
.join("")
) {
chunk.push(array[k]);
}
}

if (chunk.length != 0) {
finalArray.push(chunk.sort());
}
counter++;
}
return finalArray;
}
``````

### Conclusion

Interview questions like this one we just solved tends to test how far you have dived into algorithm. As you must have noted, the solution to just this problem is built upon other 5 algorithms we have solved in the past. So starting from the basics is very important.

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.

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

## Discussion (5) Wojciech Rebis

Great article, and good interview question.
Good work, keep it up! :)

Just wanted to share my approach to this:

``````function groupAnagrams(anagrams) {
const wordGroups = anagrams.reduce((groups, anagram) => {
const word = anagram.split('').sort().join('');
if (groups[word]) {
groups[word].push(anagram);
} else {
groups[word] = [anagram];
}
return groups;
}, {});

return Object.values(wordGroups).map(group => group);
}
``````

What do you think about it? NJOKU SAMSON EBERE

Thank you Wojciech. I love your method. It's very clever and coincise. Works perfectly too. 