I read this medium article and I understand how to get permutations of values in one array. However I need to get the permutations of dynamically entered arrays. I want to make an app where I have multiple fields where I can enter multiple arrays with many values in them and I want to get all permutations after storing them.
How can I get permutations of many different arrays with different number of values in them following the principles in this code?
function permute(nums) {
let result = [];
if (nums.length === 0) return [];
if (nums.length === 1) return [nums];
for (let i = 0; i < nums.length; i++) {
const currentNum = nums[i];
const remainingNums = nums.slice(0, i).concat(nums.slice(i + 1));
const remainingNumsPermuted = permute(remainingNums);
for (let j = 0; j < remainingNumsPermuted.length; j++) {
const permutedArray = [currentNum].concat(remainingNumsPermuted[j]);
result.push(permutedArray);
}
}
return result;
}
console.log(permute([1,2,3]))
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment