ipermutations - nodef/extra-array GitHub Wiki

List all possible permutations.

Alternatives: ipermutations, permutations. Similar: randomPermutation, permutations, hasPermutation.

function ipermutations(x, n)
// x: an array
// n: number of values [-1 ⇒ any]
const xarray = require('extra-array');

[...xarray.ipermutations([1, 2])];
// → [ [], [ 1 ], [ 2 ], [ 1, 2 ], [ 2, 1 ] ]

[...xarray.ipermutations([1, 2, 3])];
// → [
//   [],          [ 1 ],
//   [ 2 ],       [ 3 ],
//   [ 1, 2 ],    [ 1, 3 ],
//   [ 2, 1 ],    [ 2, 3 ],
//   [ 3, 1 ],    [ 3, 2 ],
//   [ 1, 2, 3 ], [ 1, 3, 2 ],
//   [ 2, 1, 3 ], [ 2, 3, 1 ],
//   [ 3, 1, 2 ], [ 3, 2, 1 ]
// ]

[...xarray.ipermutations([1, 2, 3], 2)];
// → [ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ] ]

References