Combinations

--- title: 'Combinations' author: 'μž„ν›ˆ' date: 2024-10-15T16:53:04+09:00 category: ['POSTS'] tags: ['Algorithm', 'Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- ```js function getCombinations(arr, selectNumber) { const results = []; if (selectNumber === 1) { return arr.map((value) => [value]); // Return each element as an array } arr.forEach((fixed, index, array) => { const rest = array.slice(index + 1); // The array after the current element const combinations = getCombinations(rest, selectNumber - 1); // Recursive call const attached = combinations.map((combination) => [fixed, ...combination]); // Attach the current element to the combinations results.push(...attached); }); return results; } ```
Β 
--- title: 'μ‘°ν•©(Combinations)' author: 'μž„ν›ˆ' date: 2024-10-15T16:53:04+09:00 category: ['POSTS'] tags: ['Algorithm', 'Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- ```js function getCombinations(arr, selectNumber) { const results = []; if (selectNumber === 1) { return arr.map((value) => [value]); // ν•˜λ‚˜μ”© λ°˜ν™˜ } arr.forEach((fixed, index, array) => { const rest = array.slice(index + 1); // ν˜„μž¬ μš”μ†Œ μ΄ν›„μ˜ λ°°μ—΄ const combinations = getCombinations(rest, selectNumber - 1); // μž¬κ·€ 호좜 const attached = combinations.map((combination) => [fixed, ...combination]); // ν˜„μž¬ μš”μ†Œμ™€ 쑰합을 λΆ™μž„ results.push(...attached); }); return results; } ```