I was doing a challenge from the Interview Prep section https://www.freecodecamp.org/learn/coding-interview-prep/algorithms/no-repeats-please. I wrote a function that does the job but does it so horribly that it still fails two of the conditions of the test, even though it still does return the correct results…after 46 seconds. But that’s for 7 letters, for a smaller number of letters it works ok. Some code editors will crash if you use 7 letters though, so use a sturdy one like Programiz if you want to run this code. My question is, can this code be modified to pass the test, while still preserving the logic?

```
function permAlone(arg) {
let length = arg.length
let arr = []
function uniqueIndices() { // generate a set of non-repeating integers to represent indices
let indices = new Array(length)
for (let i = 0; i < indices.length; i++) {
let r = Math.floor(Math.random() * length)
if (indices.indexOf(r) === -1) {
indices[i] = r
} else {
i--
}
}
return indices
}
function factorial(n) {
let product = 1;
for (let i = 2; i <= n; i++) {
product *= i;
}
return product;
}
let length2 = factorial(arg.length) // factorialize the length of arg to set the length of next loop which will be filled with sets of non-repeating integers
for (let i = 0; i < length2; i++) {
let val = uniqueIndices(arg)
if (arr.map(a => a.join('')).indexOf(val.join('')) === -1) { // make sure the sets of integers are all unique
arr[i] = val
} else {
i--
}
}
for (let indices of arr) { // now that we have the unique sets of non-repeating indices, convert each integer in the set to a letter in arg with the corresponding index
for (let i = 0; i < indices.length; i++) {
indices[i] = arg[indices[i]]
}
}
function final(a) { // final function to record the number of sets that have repeated consecutive letters
let ct = 0;
for (let x of a) {
for (let i = 0; i < x.length; i++) {
if (x[i] === x[i + 1]) {
ct++
break;
}
}
}
return ct
}
// to find the number of sets without repeated consecutive letters, substract their number from arr.length
return arr.length - final(arr)
}
console.log(permAlone(['a', 'b', 'c', 'd', 'e']))```
```