Skip to content

Instantly share code, notes, and snippets.

@bsa7
Created May 11, 2018 13:02
Show Gist options
  • Save bsa7/43e30553b1f9ead20fb132ac2dd22b28 to your computer and use it in GitHub Desktop.
Save bsa7/43e30553b1f9ead20fb132ac2dd22b28 to your computer and use it in GitHub Desktop.
Найти линейным способом индексы похожих элементов в массиве, не изменяя сам массив
var arr = ["abc", "bac","abc", "d","et","d","et","zzz"];
const unorderedStat = {}
arr.forEach((item, index) => {
const key = item.split('').sort().join()
if (!unorderedStat[key]) {
unorderedStat[key] = [index]
} else {
unorderedStat[key].push(index)
}
})
Object.keys(unorderedStat).forEach((key) => {
if (unorderedStat[key].length > 1) {
console.log(`${key}: ${unorderedStat[key].join(', ')}`)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment