[Solved] Quick and efficient permutations with max length


Vocabulary lesson: these are actually combinations, not permutations. With permutations, a different order is a different permutations. Also, what you want technically isn’t all combinations, as there is the empty combination [] and it seems you don’t want that included.

Anyway, here’s something I whipped up.

function getCombinations(array) {
    let combinations = [];
    for (let value of array) {
        combinations = combinations
            .concat(combinations.map(value0 => value0.concat([value])))
            .concat([[value]]);
    }
    
    return combinations;
}

console.log(getCombinations([0, 1, 2, 3]))

1

solved Quick and efficient permutations with max length