Skip to content

Instantly share code, notes, and snippets.

@yosvelquintero
Last active June 13, 2020 00:56
Show Gist options
  • Save yosvelquintero/331ed957199c56f7c0c2d0eedafc3d06 to your computer and use it in GitHub Desktop.
Save yosvelquintero/331ed957199c56f7c0c2d0eedafc3d06 to your computer and use it in GitHub Desktop.
/**
* groupWordsByHavingSameLetters
*
* Group the below words by having the same letter but different position
*
* @param {Array<string>} arr
* @returns {Array<string>}
*/
const groupWordsByHavingSameLetters = arr =>
Object
.values(
arr.reduce((a, c) => {
const sortedWord = c
.split('')
.sort()
.join('')
a[sortedWord] = a[sortedWord] || []
a[sortedWord].push({ word: c })
return a
}, {})
)
.map(matchArr =>
matchArr.map(item => item.word).join(' - ')
)
const data = ['AMOR','XISELA','JAMON','ROMA','OMAR','MORA','ESPONJA','RAMO','JAPONES','ARMO','MOJAN','MARO','ORAM', 'MONJA','ALEXIS']
groupWordsByHavingSameLetters(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment