forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombineWithoutRepetitions.js
More file actions
37 lines (32 loc) · 948 Bytes
/
combineWithoutRepetitions.js
File metadata and controls
37 lines (32 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @param {*[]} comboOptions
* @param {number} comboLength
* @param {*[][]} combos
* @param {*[]} currentCombo
* @return {*[]}
*/
function combineRecursively(comboOptions, comboLength, combos = [], currentCombo = []) {
if (comboLength === 0) {
combos.push(currentCombo);
return combos;
}
for (let letterIndex = 0; letterIndex <= (comboOptions.length - comboLength); letterIndex += 1) {
const letter = comboOptions[letterIndex];
const restCombinationOptions = comboOptions.slice(letterIndex + 1);
combineRecursively(
restCombinationOptions,
comboLength - 1,
combos,
currentCombo.concat([letter]),
);
}
return combos;
}
/**
* @param {*[]} combinationOptions
* @param {number} combinationLength
* @return {*[]}
*/
export default function combineWithoutRepetitions(combinationOptions, combinationLength) {
return combineRecursively(combinationOptions, combinationLength);
}