function permutations(string) {
  // Función recursiva para generar permutaciones
  function generatePerms(str) {
      
    if (str.length <= 1) {
        // console.log('generatePerms')
      return [str];
    }

    let allPerms = [];
      
    for (let i = 0; i < str.length; i++) {
      const char = str[i];
        console.log("second", str, str.slice(0, i), str.slice(i + 1), 'i=', i)
        
      const remainingChars = str.slice(0, i) + str.slice(i + 1);
        // console.log(remainingChars)
      const permsOfRemaining = generatePerms(remainingChars);
        console.log("permsOfRemaining", permsOfRemaining)
      for (const perm of permsOfRemaining) {
          console.log('perm', perm, 'char', char, 'i=', i)
        allPerms.push(char + perm);
      }
    }
    return allPerms;
  }

  // Generar todas las permutaciones y eliminar duplicados usando un Set
  const allPermutations = generatePerms(string);
    console.log(allPermutations)
  const uniquePermutations = [...new Set(allPermutations)];

  return uniquePermutations;
}

// Ejemplos de uso:
// console.log(permutations('a')); // ['a']
// console.log(permutations('ab')); // ['ab', 'ba']
console.log(permutations('abc')); // ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
// console.log(permutations('aabb')); // ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa']


// other way the best options

function permutations(str) {
 return (str.length <= 1) ? [str] :
         Array.from(new Set(
           str.split('')
              .map((char, i) => permutations(str.substr(0, i) + str.substr(i + 1)).map(p => char + p))
              .reduce((r, x) => r.concat(x), [])
         ));
}


// to leanrd ...

const unique = xs => [ ...new Set(xs) ]
const concat = (a, b) => [ ...a, ...b ] 
const drop = i => xs => [ ...xs.slice(0, i), ...xs.slice(i + 1) ]

const permute = (x, i, xs) => 
  combinations(drop(i)(xs)).map(y => x + y)

const combinations = s =>
  s.length === 1 ? [ s ] : [ ...s ].map(permute).reduce(concat)

const permutations = s => unique(combinations(s))

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: