Last active
March 6, 2018 07:30
-
-
Save gongpeione/2d1f4ba058775d03ae4892f958680fcd to your computer and use it in GitHub Desktop.
AllSets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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