const makingComb = (arr, leng) => {
const result = [];
if(leng === 1) return arr.map(i => [i]);
arr.forEach((fixed, idx, arr) => {
const combination = makingComb(arr.slice(idx + 1), leng - 1);
const attached = combination.map(i => [fixed, ...i]);
result.push(...attached);
})
return result;
}
makingComb([1, 2, 3, 1, 2, 3, 4], 4);
//result
[
[ 1, 2, 3, 1 ], [ 1, 2, 3, 2 ], [ 1, 2, 3, 3 ],
[ 1, 2, 3, 4 ], [ 1, 2, 1, 2 ], [ 1, 2, 1, 3 ],
[ 1, 2, 1, 4 ], [ 1, 2, 2, 3 ], [ 1, 2, 2, 4 ],
[ 1, 2, 3, 4 ], [ 1, 3, 1, 2 ], [ 1, 3, 1, 3 ],
[ 1, 3, 1, 4 ], [ 1, 3, 2, 3 ], [ 1, 3, 2, 4 ],
[ 1, 3, 3, 4 ], [ 1, 1, 2, 3 ], [ 1, 1, 2, 4 ],
[ 1, 1, 3, 4 ], [ 1, 2, 3, 4 ], [ 2, 3, 1, 2 ],
[ 2, 3, 1, 3 ], [ 2, 3, 1, 4 ], [ 2, 3, 2, 3 ],
[ 2, 3, 2, 4 ], [ 2, 3, 3, 4 ], [ 2, 1, 2, 3 ],
[ 2, 1, 2, 4 ], [ 2, 1, 3, 4 ], [ 2, 2, 3, 4 ],
[ 3, 1, 2, 3 ], [ 3, 1, 2, 4 ], [ 3, 1, 3, 4 ],
[ 3, 2, 3, 4 ], [ 1, 2, 3, 4 ]
]
출처1
출처2