Last active
March 26, 2024 13:39
-
-
Save inca/4f00677bc3947c104e21f1721a1f8f41 to your computer and use it in GitHub Desktop.
Generate Combinations Without Repetition
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* 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