gistlib
index.tsxfunction getAllCombinations(arr) { let result = []; // Helper function to generate combinations function generateCombinations(subArr, index) { result.push(subArr); for (let i = index; i < arr.length; i++) { generateCombinations([...subArr, arr[i]], i + 1); } } generateCombinations([], 0); return result; } const mySet = [1, 2, 3]; const allCombinations = getAllCombinations(mySet); console.log(allCombinations); 491 chars22 lines
function getAllCombinations(arr) { let result = []; // Helper function to generate combinations function generateCombinations(subArr, index) { result.push(subArr); for (let i = index; i < arr.length; i++) { generateCombinations([...subArr, arr[i]], i + 1); } } generateCombinations([], 0); return result; } const mySet = [1, 2, 3]; const allCombinations = getAllCombinations(mySet); console.log(allCombinations);
gistlibby LogSnag