Print all Permutation of Array using JavaScript
Last Updated :
08 May, 2024
Permutations of Array are a fundamental concept in combinatorics and algorithms, showcasing all possible arrangements of its elements.
Examples:
Input: nums = [1, 2, 3]
Output: [ [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2] ]
Explanation: There are 6 possible permutations
Input: nums = [1, 3]
Output: [ [1, 3], [3, 1] ]
Explanation: There are 2 possible permutations
Below are the approaches to Print all permutations of arrays using JavaScript.
Using Recursion
- In this approach, we are using recursion to generate all permutations of the input array.
- Starting from the first element, we swap elements at different positions and recursively generate permutations until all possible combinations are explored, storing each permutation in the res array.
Example: The below example uses Recursion to Print all permutations of an array using JavaScript.
JavaScript
let arr = [1, 2, 3];
let res = [];
function approach1Fn(arr, start) {
if (start === arr.length - 1) {
res.push([...arr]);
return;
}
for (let i = start; i < arr.length; i++) {
[arr[start], arr[i]] = [arr[i], arr[start]];
approach1Fn(arr, start + 1);
[arr[start], arr[i]] = [arr[i], arr[start]];
}
}
approach1Fn(arr, 0);
console.log(res);
Output[
[ 1, 2, 3 ],
[ 1, 3, 2 ],
[ 2, 1, 3 ],
[ 2, 3, 1 ],
[ 3, 2, 1 ],
[ 3, 1, 2 ]
]
Time Complexity: O(n!), where n is the number of elements in the array.
Space Complexity: O(n!)
Using Iterative Method
- In this approach, we are using an iterative method to generate all permutations of the input array.
- By inserting each array element at different positions in each existing permutation, we build up a new set of permutations until all combinations are explored and stored in the res array.
Example: The below example uses the Iterative Method to Print all permutations of an array using JavaScript.
JavaScript
let arr = [1,3];
let res = [[]];
for (let num of arr) {
const temp = [];
for (let arr of res) {
for (let i = 0; i <= arr.length; i++) {
const newArr = [...arr];
newArr.splice(i, 0, num);
temp.push(newArr);
}
}
res = temp;
}
console.log(res);
Output[ [ 3, 1 ], [ 1, 3 ] ]
Time Complexity: O(n!), where n is the number of elements in the array.
Space Complexity: O(n!)
Similar Reads
Generate a Random Permutation of 1 to n in JavaScript In programming, creating a random permutation of numbers from 1 to n is a common task. A random permutation means randomly rearranging the elements that make up a permutation. Below are the methods to generate a random permutation of 1 to n in JavaScript: Table of Content Fisher-Yates (Knuth) Algori
2 min read
How to Shuffle an Array using JavaScript ? To shuffle a JavaScript array we can use the Fisher-Yates shuffle also known as knuth shuffle. It will sort the given array in a random order with the help of the math.random() function.1. Shuffle JavaScript Array Using Fisher-Yates ShuffleThe Fisher-Yates Shuffle iterates through the array in rever
3 min read
Rotate an Array to the Right by K Steps using JavaScript One can Rotate an array to the left by k steps using JavaScript. We are given an input array and a value k we have to rotate that array by k steps in the right direction.Example:Input arr = [1 , 2 , 3 , 4 , 5 ]k = 3 Output arr = [ 3 , 4 , 5 , 1 , 2]Below are different approaches to Rotate an array t
4 min read
How to Move a Key in an Array of Objects using JavaScript? The JavaScript array of objects is a type of array that contains JavaScript objects as its elements.You can move or add a key to these types of arrays using the below methods in JavaScript:Table of ContentUsing Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() met
5 min read
Swapping two array elements in a single line using JavaScript In JavaScript, there exist many ways by which one can swap two array elements. In this article, we will discuss a way in which one can swap two array elements in JavaScript in a single line. The input and output would be as follows. Input: arr = { 10, 20, 40, 30 }Output: arr = { 10, 20, 30, 40 } //
3 min read
Check if two strings are permutation of each other in JavaScript In this approach, we are going to discuss how we can check if two strings are permutations of each other or not using JavaScript language. If two strings have the same number of characters rather than having the same position or not it will be a permutation. Example: Input: "pqrs" , "rpqs"Output: Tr
4 min read