Skip to content

Instantly share code, notes, and snippets.

@chrisnatali
Created June 24, 2015 20:59
Show Gist options
  • Save chrisnatali/027c47dd37e5f5ed046e to your computer and use it in GitHub Desktop.
Save chrisnatali/027c47dd37e5f5ed046e to your computer and use it in GitHub Desktop.
iterative js powerset
function powerset(input) {
var result = [];
var selections; // bitmap of current subset
for(selections = 0; selections < Math.pow(2, input.length); selections++) {
var subset = [];
var index; // index into bitmap we're looking for
for(index = 0; index < input.length; index++) {
if(selections & (1 << index)) {
subset.push(input[index]);
}
}
result.push(subset);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment