Last active
February 14, 2018 12:07
-
-
Save io-developer/8399fc286bffbf28784bd374f3f9892f to your computer and use it in GitHub Desktop.
Sets and combinations
This file contains 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 findSetsIn(list, noEmptySet) { | |
var result = noEmptySet ? [] : [[]]; | |
for (var i = 0; i < list.length; i++) { | |
var base = [ list[i] ]; | |
result.push(base); | |
var tail = findSetsIn(list.slice(i + 1), true); | |
for (var j = 0; j < tail.length; j++) { | |
result.push(base.concat(tail[j])); | |
} | |
} | |
return result; | |
} | |
findSetsIn([1, 2, 3, 4, 5]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment