Created
October 5, 2018 10:37
-
-
Save gka/070dca80d4d7c25f903c00089823d3f1 to your computer and use it in GitHub Desktop.
Returns all possible combinations of the elements in a given array
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 combinations(arr) { | |
var fn = function(active, rest, a) { | |
if (!active.length && !rest.length) | |
return; | |
if (!rest.length) { | |
a.push(active); | |
} else { | |
fn(active.concat(rest[0]), rest.slice(1), a); | |
fn(active, rest.slice(1), a); | |
} | |
return a; | |
} | |
return fn([], arr, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment