Dev HelpersOpen library
← Back

Generate a powerset

Generate a powerset. A self-contained example using the JavaScript runtime.

Open this example in the interactive library

Source code

console.log(
  [1, 2, 3].reduce(
    (sets, x) => sets.concat(sets.map((s) => [...s, x])),
    [[]],
  ),
);

Sample output

[
  [],       [ 1 ],
  [ 2 ],    [ 1, 2 ],
  [ 3 ],    [ 1, 3 ],
  [ 2, 3 ], [ 1, 2, 3 ]
]