Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all the combinations of N elements of multidimensional array

I'm trying to write an algorithm to get all the possible combinations of N elements inside a multi dimensional array of M elements.

Something like:

function getCombinations(arr, n){
  ...
}

var arr = [ ["A"],["B","C"],["D","E"]];
var n = 2;

getCombinations(arr,n);

This should produce:

[
["A","B"],["A","C"],["A","D"],["A","E"],
["B","D"],["B","E"],
["C","D"],["C","E"]
]

The number of elements inside the array may vary, the only thing set is the number of elements of the combinations.

The order doesn't matter but you cannot repeat, I mean ["A","B"] == ["B","A"], so the second one is not take in consideration.

Any help?

like image 260
Javier Cobos Avatar asked Dec 20 '22 01:12

Javier Cobos


1 Answers

ChrisB solution had a mistake, he wasn't caching the length of the loop before the arr.shift, and it was not returning the last combination, I think this will do the job:

function getCombinations (arr, n) {
    var i, j, k, elem, l = arr.length, childperm, ret = [];
    if (n == 1){
        for (i = 0; i < arr.length; i++) {
            for (j = 0; j < arr[i].length; j++) {
                ret.push([arr[i][j]]);
            }
        }
        return ret;
    }
    else {
        for (i = 0; i < l; i++) {
            elem = arr.shift();
            for (j = 0; j < elem.length; j++) {
                childperm = getCombinations(arr.slice(), n-1);
                for (k = 0; k < childperm.length; k++) {
                    ret.push([elem[j]].concat(childperm[k]));
                }
            }
        }
        return ret;
    }
}


getCombinations([["A"],["B"],["C","D"]], 2);

// [["A", "B"], ["A", "C"], ["A", "D"], ["B", "C"], ["B", "D"]]

getCombinations([["A"],["B"],["C"],["D"]], 2);

// [["A", "B"], ["A", "C"], ["A", "D"], ["B", "C"], ["B", "D"], ["C", "D"]]
like image 166
Javier Cobos Avatar answered Jan 13 '23 12:01

Javier Cobos