Skip to content

Instantly share code, notes, and snippets.

@kevinji
Created December 13, 2017 21:48
Show Gist options
  • Save kevinji/5521a94f64dc408cced9438bdfb4e1c3 to your computer and use it in GitHub Desktop.
Save kevinji/5521a94f64dc408cced9438bdfb4e1c3 to your computer and use it in GitHub Desktop.
Find the powerset of an array.
// Source: https://stackoverflow.com/a/42774138/558592 (slightly tweaked)
function powerset(arr) {
return (Array.from({ length: 1 << arr.length }, (_, k) => k)
.map(i => arr.filter((_, j) => i & (1 << j))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment