Skip to content

Instantly share code, notes, and snippets.

@sandro-pasquali
Last active November 10, 2015 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandro-pasquali/87d1fb0ccd34de09d914 to your computer and use it in GitHub Desktop.
Save sandro-pasquali/87d1fb0ccd34de09d914 to your computer and use it in GitHub Desktop.
function powerSet(list) {
var set = [];
var listSize = list.length;
var combinationsCount = 1 << listSize;
var combination;
var i = 1;
var j;
for(; i < combinationsCount; i++ ) {
var combination = [];
for(j=0; j<listSize; j++) {
if(i & 1 << j){
combination.push(list[j]);
}
}
set.push(combination);
}
return set;
}
console.log(powerSet(['a','b','c','d']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment