I have a simple permute function. It works well with arrays that have less than 5 items. But when I have 40 items in the array, my web is crashed. I used to try to split that large array into small sub-arrays, but it missed many cases. Are there other ways to solve this problem?
const permute = (items) => {
const result = [];
const dsf = (i, items) => {
if (i === items.length) {
result.push(items.slice())
return;
}
for (let j = 0; j < items.length; j++) {
[items[i], items[j]] = [items[j], items[i]];
dsf(i + 1, items);
[items[i], items[j]] = [items[j], items[i]];
}
}
dsf(0, items);
return result;
}