I have different arrays, all with numbers, but with different number of elements:
var ar1 = [2, 5];
var ar2 = [1, 2, 3];
I need to get all permutations for each array. The length of the output elements should always be the same as the input array.
This result should be an array of arrays, like this:
for ar1:
[2, 5]
[5, 2]
for ar2:
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
I don't want a cartesian product, each array should be processed on its own.
All solutions I found so far are creating only order independent arrays, so the result for ar1 is only one array and not two.
The Solution should work for any number of elements in the input array. We can assume that there are no duplicate values in the input array.