Skip to content

Instantly share code, notes, and snippets.

@zlrlo
Created October 3, 2023 02:41
Show Gist options
  • Select an option

  • Save zlrlo/bd016a65128dd6baeedef6f0601d93ec to your computer and use it in GitHub Desktop.

Select an option

Save zlrlo/bd016a65128dd6baeedef6f0601d93ec to your computer and use it in GitHub Desktop.
[JS] 조합 구현
function combinations(arr, n) {
// 1개만 뽑는다면 그대로 조합을 반환한다. 탈출 조건으로도 사용된다.
if (n === 1) return arr.map((v) => [v]);
const result = [];
// 요소를 순환한다
arr.forEach((fixed, idx, arr) => {
// 현재 index 이후 요소를 추출한다.
// index번째는 선택된 요소
const rest = arr.slice(idx + 1);
// 선택된 요소 이전 요소들을 제외하고 재귀 호출한다.
const combis = combinations(rest, n - 1);
// 선택된 요소와 재귀 호출을 통해 구한 조합을 합쳐준다.
const combine = combis.map((v) => [fixed, ...v]);
// 결과 값을 추가한다.
result.push(...combine);
});
// 결과 반화
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment