Created
May 11, 2018 13:02
-
-
Save bsa7/43e30553b1f9ead20fb132ac2dd22b28 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
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