-
Notifications
You must be signed in to change notification settings - Fork 224
/
combinations.js
34 lines (31 loc) · 1.02 KB
/
combinations.js
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
/**
* Implementation of Combinations
* Combinations are unique subsets of a collection - in this case, k x from a collection at a time.
* https://en.wikipedia.org/wiki/Combination
* @param {Array} x any type of data
* @param {int} k the number of objects in each group (without replacement)
* @returns {Array<Array>} array of permutations
* @example
* combinations([1, 2, 3], 2); // => [[1,2], [1,3], [2,3]]
*/
function combinations(x, k) {
let i;
let subI;
const combinationList = [];
let subsetCombinations;
let next;
for (i = 0; i < x.length; i++) {
if (k === 1) {
combinationList.push([x[i]]);
} else {
subsetCombinations = combinations(x.slice(i + 1, x.length), k - 1);
for (subI = 0; subI < subsetCombinations.length; subI++) {
next = subsetCombinations[subI];
next.unshift(x[i]);
combinationList.push(next);
}
}
}
return combinationList;
}
export default combinations;