[JavaScript] 재귀함수로 combination 구하기

김정호·2022년 3월 7일
function getCombinations(arr, selectNumber) {
	if (selectNumber === 1) return arr.map((x) => [x])

	const results = []
	arr.forEach((fixed, index, origin) => {
		const rest = origin.slice(index + 1)
		const combinations = getCombinations(rest, selectNumber - 1)
		const attached = combinations.map((x) => [fixed, ...x])
		results.push(...attached)
	})
	return results
}
profile
개발자

0개의 댓글