Last active
June 13, 2020 00:56
-
-
Save yosvelquintero/331ed957199c56f7c0c2d0eedafc3d06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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