0

Lets say i have an array with 3 numbers:

[1,2,3]

I want to find all combinations of this 3 numbers where all the numbers are included.

Output should look like this:

[1,2,3], [1,3,2], [3,1,2], [3,2,1], [2,3,1], [2,1,3]
Herbo
  • 1
  • 1
  • https://stackoverflow.com/questions/42773836/how-to-find-all-subsets-of-a-set-in-javascript#:~:text=You%20can%20easily%20generate%20the,%3D%200%3B%20j%20%3C%20array. – freqnseverity Jul 01 '20 at 02:03
  • i need only combinations including all 3 numbers and also i need any given combination of them. 3,2,1 ≠ 1,2,3. Also i dont need 1,1,2 or 1,2,2 etc. Every number can only occure once. – Herbo Jul 01 '20 at 02:07
  • https://stackoverflow.com/a/9960925/636077 – ray hatfield Jul 01 '20 at 02:12

1 Answers1

0

It's a bit brute force, but if you're only going to have to do this for a list of three elements, this is simple enough.

a = [1, 2, 3]
a_comb = [[a[0],a[1],a[2]], [a[0], a[2], a[1]], [a[2], a[0], a[1]], [a[2], a[1], a[0]], [a[1],a[0],a[2]], [a[1],a[2],a[0]]]