Skip to content

Instantly share code, notes, and snippets.

@gongpeione
Last active March 6, 2018 07:30
Show Gist options
  • Save gongpeione/2d1f4ba058775d03ae4892f958680fcd to your computer and use it in GitHub Desktop.
Save gongpeione/2d1f4ba058775d03ae4892f958680fcd to your computer and use it in GitHub Desktop.
AllSets
function allSets (arr) {
const target = Math.pow(2, arr.length) - 1;
const all = [];
for (let i = 1; i <= target; i++) {
all.push(Array.from({length: arr.length}, (v, index) => i & (1 << index) ? arr[index] : null).filter(v => arr.indexOf(v) > -1));
}
console.log(all);
}
function allSetsRec (arr, index, set, all) {
if (index === arr.length) {
all.push(set);
return;
}
all = all || [];
index = index || 0;
set = set || [];
as(arr, index + 1, set, all);
as(arr, index + 1, set.concat(arr[index]), all);
console.log(all);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment