Skip to content

permutations

Subhajit Sahu edited this page Feb 2, 2021 · 21 revisions

Lists all possible permutations. 🏃 📼 📦 🌔 📒

Similar: permutation, permutations, hasPermutation.


array.permutations(x, [n]);
// x: an array
// n: number of values (-1 => any)
const array = require('extra-array');

[...array.permutations([1, 2])];
// [ [], [ 1 ], [ 2 ], [ 1, 2 ], [ 2, 1 ] ]

[...array.permutations([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 ]
// ]

[...array.permutations([1, 2, 3], 2)];
// [ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ] ]


References

Clone this wiki locally