/* @flow */ /** * 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 of permutations * @example * combinations([1, 2, 3], 2); // => [[1,2], [1,3], [2,3]] */ function combinations( x /*: Array */, k /*: number */ ) /*: Array> */ { 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;