Skip to content

Instantly share code, notes, and snippets.

@tiotamara
Last active October 19, 2022 15:34
Show Gist options
  • Save tiotamara/165cdae1a9bb265a83231a1535cb836f to your computer and use it in GitHub Desktop.
Save tiotamara/165cdae1a9bb265a83231a1535cb836f to your computer and use it in GitHub Desktop.
Logic Test
const anagrams = (arr) => {
const result = {}
for (value of arr) {
const key = value.split('').sort().join('')
if (typeof result[key] === 'undefined') {
result[key] = []
}
result[key].push(value)
}
return Object.values(result)
}
const arr = ['cook', 'save', 'taste', 'aves', 'vase', 'state', 'map']
const res = anagrams(arr)
console.log(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment