function permutations(string) { return string.length === 1 ? [string] : [...new Set([...string].map((item, index) => { return permutations(string.slice(0, index) + string.slice(index + 1)).map(val => item + val).join(','); }).join(',').split(','))]; } console.log(permutations("abc")); console.log(permutations("aabb"));