get permutation js recursion recursion code example
Example: getpermutation js recursion
(1) function permute(nums) {(2) let result = [];(3) if (nums.length === 0) return [];(4) if (nums.length === 1) return [nums];(5) for (let i = 0; i < nums.length; i++) {(6) const currentNum = nums[i];(7) const remainingNums = nums.slice(0, i).concat(nums.slice(i + (8) 1));(9) const remainingNumsPermuted = permute(remainingNums);(10) for (let j = 0; j < remainingNumsPermuted.length; j++) {(11) const permutedArray = (12) [currentNum].concat(remainingNumsPermuted[j]);(13) result.push(permutedArray);(14) }(15) }(16) return result;(17) }