Skip to content

Instantly share code, notes, and snippets.

@ajalab
Created November 7, 2010 14:56
Show Gist options
  • Save ajalab/666164 to your computer and use it in GitHub Desktop.
Save ajalab/666164 to your computer and use it in GitHub Desktop.
Combination
function combination(array, len) {
var buf = [], retarr = [];
function sub(arr, buf) {
var subarr, subbuf;
var max = arr.length - (len - buf.length);
for (var i = 0; i < arr.length; i++) {
subarr = arr.slice();
subbuf = buf.slice();
subbuf.push(subarr[i]);
subarr.splice(0, i + 1);
if (subbuf.length >= len) {
retarr.push(subbuf);
} else {
sub(subarr, subbuf);
}
}
}
sub(array, []);
return retarr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment