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