Skip to content

Instantly share code, notes, and snippets.

@gka
Created October 5, 2018 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gka/070dca80d4d7c25f903c00089823d3f1 to your computer and use it in GitHub Desktop.
Save gka/070dca80d4d7c25f903c00089823d3f1 to your computer and use it in GitHub Desktop.
Returns all possible combinations of the elements in a given array
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