how to get all combinations of a set in javascript in javascript

index.tsx
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);
491 chars
22 lines

related categories

gistlibby LogSnag