A permutation is one ordering of a collection, and a subset is any selection from it — from picking nothing up to picking everything. This question asks you to generate all of both for an array of distinct values: every permutation (there are n! of them) and every subset (the power set, 2^n of them), packaged on one object as permutationsSubsets = { permutations, subsets }. The reason to pair them is that a single backtracking template — choose an option, recurse, then undo the choice — produces both; only the meaning of a choice changes. See Permutation and Power set for background.
permutationsSubsets.permutations(arr) // distinct items -> array of all n! orderings
permutationsSubsets.subsets(arr) // distinct items -> array of all 2^n subsets
Both return an array of arrays; each inner array is a brand-new array.
permutationsSubsets.permutations([1, 2, 3]);
// six arrays, in some order:
// [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]
permutationsSubsets.subsets([1, 2, 3]);
// eight arrays, in some order:
// [], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]
permutations([]) is [[]] (one empty ordering) and subsets([]) is [[]] (the empty array is the only subset). Both have exactly one element, not zero.n! and 2^n blow up quickly (10! is over three million), so this is meant for small n. No libraries — build both yourself.Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.