Skip to content

Instantly share code, notes, and snippets.

@scarletquasar
Created October 4, 2023 19:18
Show Gist options
  • Save scarletquasar/4c7dc9e64976db6395d6731346b8c0cc to your computer and use it in GitHub Desktop.
Save scarletquasar/4c7dc9e64976db6395d6731346b8c0cc to your computer and use it in GitHub Desktop.
sorted words
const words = ["cat", "act", "tac", "art", "tar"];
const expectedOutput = {
"act": ["cat", "act", "tac"],
"atr": ["art", "tar"],
}
const sortedWords = words
.map(word => word.split(''))
.map(array => array.sort())
.map(array => array.join(''));
const groups: Record<string, string[]> = {};
for (const word of words) {
groups[word.split('').sort().join('')] = [];
}
sortedWords.forEach((word, index) => {
groups[word].push(words[index]);
})
console.log(groups)
console.log(expectedOutput)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment