Skip to content

Instantly share code, notes, and snippets.

@inca
Last active March 26, 2024 13:39
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 inca/4f00677bc3947c104e21f1721a1f8f41 to your computer and use it in GitHub Desktop.
Save inca/4f00677bc3947c104e21f1721a1f8f41 to your computer and use it in GitHub Desktop.
Generate Combinations Without Repetition
function* generateCombinations(array) {
if (array.length == 0) {
return;
}
const head = array[0];
const tail = array.slice(1);
yield [head];
for (const comb of generateCombinations(tail)) {
yield comb;
yield [head].concat(comb);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment