Created
June 29, 2018 00:17
-
-
Save pinkmomo027/7f2a0b2fadec6c658801dac32b659f7e to your computer and use it in GitHub Desktop.
subsets of array
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
let names = ["fifi", "vampire", "lena", "wheels"]; | |
let finalResult = []; | |
function sublist(array) { | |
let chosen = []; | |
sublistHelper(array, chosen); | |
} | |
function sublistHelper(array, chosen) { | |
if (array.length == 0) { | |
// base case | |
finalResult.push(Object.assign([], chosen)); | |
} else { | |
let currentElement = array.shift(); | |
// if select this element and explore | |
chosen.push(currentElement); | |
sublistHelper(array, chosen); | |
// if not select this element and explore | |
chosen.pop(); | |
sublistHelper(array, chosen); | |
array.unshift(currentElement); | |
} | |
} | |
sublist(names); | |
console.log(finalResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.