javascriptalgorithmrecursionpermutation# Find all permutations of an array - Where am I going wrong?

So we have an array and I need to find all the permutations of the items in it.

for e.g, `arr = [1, 2] should return [[1, 2], [2,1]]`

Now here's my thought process:

If the array is empty, it should return `[]`

which gives me the base condition.

`if(arr.length === 0) return [];`

Now we can further break the problem like this:

`permutation([item1, item2]) = [[item1, ...permutation(item2)], [item2, ...permutation(item1)]`

`// permutation([1, 2, 3]) = [[1, ...permutation(2,3)], [2, ...permutation(1,3)], [3, ...permutation(1,3)]`

So with that I tried to code:

```
function findPermutation(arr){
function permute(arr){
if(arr.length === 0) return [];//empty array base condition
var res= [];
for(let el of arr){
//Reducing the array
var filteredArray = arr.filter(a=> a!== el);
//taking recursive leap of faith
var temp = permute(filteredArray);
res.push([el, ...(temp)])
}
//I suspect the problem could be here. It doesn't result
//correctly so final output remains incorrect
return res;
}
return permute(arr);
}
console.log(findPermutation([1, 2, 3]))
```

But somehow I'm making some mistakes in returning the `res`

which is resulting in giving wrong results.

Where am I going wrong and is the above thought process correct?

Solution

The approach which you're following is correct. However, there needs a slight modification in your implementation. So, when you push the element into the `res`

array, you need to push it into an array combined with each of the sub-permutations returned by the `permute(filteredArray)`

call. One way to achieve this is by using a nested loop.

**Analysis of the proposed implementation:**

When we call `permute(filteredArray)`

, it returns an array of all possible permutations of the filteredArray (our leap of faith). Since each element from the original array will have its unique set of sub-permutations, we need to iterate through all these sub-permutations and prepend the current element (`el`

) to each of these sub-permutations.
For instance, let's take a look at the array `[1, 2, 3]`

. Now, let's take `1`

under consideration:

Pick the first element

`1`

and filter the array to get`[2, 3]`

. Now we call`permute([2, 3])`

to get the sub-permutations:`[[2, 3], [3, 2]]`

(faith).We need to prepend the element

`1`

to each of the sub-permutations:`[[1, 2, 3], [1, 3, 2]]`

. This is why we use a nested loop to iterate through the`subPermutations`

returned by the`permute(filteredArray)`

call.

Using the spread syntax `...subPermutations`

without looping(as per your code) through the sub-permutations would result in an incorrect combination, as it would not cover all possible permutations for each element.

```
function findPermutation(arr) {
function permute(arr) {
if (arr.length === 0) return [
[]
]; // Return array with an empty array
const res = [];
for (let el of arr) {
const filteredArray = arr.filter(a => a !== el);
const subPermutations = permute(filteredArray);
// note the below loop
for (let subPermutation of subPermutations) {
res.push([el, ...subPermutation]);
}
}
return res;
}
return permute(arr);
}
console.log(findPermutation([1, 2, 3]));
```

- How not to overwrite user data in FireStore for returning user when login using Angularifre?
- using spread operator in typescript
- javascript postMessage not working
- Is there a way to add/remove several classes in one single instruction with classList?
- Appending .js extension on relative import statements during Typescript compilation (ES6 modules)
- How do I "preventDefault" for custom events
- React Hooks useState+useEffect+event gives stale state
- How to check if a string ONLY contains specific strings
- How to prevent changes to a prototype?
- Creating a textarea with auto-resize
- Javascript file upload - PDF to image(s)
- Truncate number to two decimal places without rounding
- How browsers store data for autocomplete and where?
- Debt Snowball Function
- JSON Web Token (JWT) Error: Invalid Signature with RSA Key Pairs
- What a RegEx that can match text in parentheses with nested parentheses
- How would I get the full response from this Axios request?
- next js - the style of the tailwind class i get from API doesnt apply on my element
- How can I disable sort on a specific element?
- Is it a bad idea to use the meta tag in HTML to specify custom metadata for retrieval with JavaScript?
- Detecting when user scrolls to bottom of div with React js
- Mongoose partial update of an object
- in javascript, how do you sort a subset of an array?
- Firebase email link authentication leads to a page that says "Error encountered" - "The selected page mode is invalid"
- How to fire window.scrollY only one time
- How to call a method from child component to parent one in vue2?
- Switch value for clicked button from group of buttons in jQuery
- Smooth scroll to specific div on click
- How to use MaterializeCss with Vue.js?
- Can't find variable: React