Uh oh!
There was an error while loading. Please reload this page.
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
Latest commit
32 lines (28 loc) · 1.02 KB
/
Copy pathcombineWithoutRepetitions.js
File metadata and controls
32 lines (28 loc) · 1.02 KB
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
/**
* @param {*[]} comboOptions
* @param {number} comboLength
* @return {*[]}
*/
exportdefaultfunctioncombineWithoutRepetitions(comboOptions,comboLength){
// If the length of the combination is 1 then each element of the original array
// is a combination itself.
if(comboLength===1){
returncomboOptions.map((comboOption)=>[comboOption]);
}
// Init combinations array.
constcombos=[];
// Extract characters one by one and concatenate them to combinations of smaller lengths.
// We need to extract them because we don't want to have repetitions after concatenation.
comboOptions.forEach((currentOption,optionIndex)=>{
// Generate combinations of smaller size.
constsmallerCombos=combineWithoutRepetitions(
comboOptions.slice(optionIndex+1),
comboLength-1,
);
// Concatenate currentOption with all combinations of smaller size.
smallerCombos.forEach((smallerCombo)=>{
combos.push([currentOption].concat(smallerCombo));
});
});
returncombos;
}