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 y...
For further actions, you may consider blocking this person and/or reporting abuse
Great article, and good interview question.
Good work, keep it up! :)
Just wanted to share my approach to this:
What do you think about it?
Thank you Wojciech. I love your method. It's very clever and coincise. Works perfectly too.
dev-to-uploads.s3.amazonaws.com/i/...
Do you mind explaining what is going on in the reduce() function probably by commenting the code?
I thought of reducing array of anagrams to an object which keys will be initial words and values arrays of anagrams from these words.
Tried pretty much in the same way
function groupAnagrams(array) {
var reducedObject = array.reduce((acc, cur) => {
var newcur=cur.split('').sort().join('');
if (!acc[newcur]) {
acc[newcur] = [cur]
} else {
acc[newcur] = [...acc[newcur], cur]
}
return acc;
}, {});
return Object.values(reducedObject);
}
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
Nice! :)
Bruno, Thank you for taking your time to read through!