Skip to content

Instantly share code, notes, and snippets.

@gka gka/combinations.js
Created Oct 5, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.