Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all possible combinations of elements in an array including order and lengths

function getAllCombinations(arr) {

  var f = function(arr) {
    var result = [];
    var temp = [];

    for (var i = 0; i < arr.length; i++) {
      temp = [];
      temp.push(arr[i]);
      result.push(temp);

      for (var j = 0; j < arr.length; j++) {
        if (j != i) {
          temp = [];
          temp.push(arr[i]);
          temp.push(arr[j]);
          result.push(temp);

          for (var k = 0; k < arr.length; k++) {
            if (k != i && k != j) {
              temp = [];
              temp.push(arr[i]);
              temp.push(arr[j]);
              temp.push(arr[k]);
              result.push(temp);

              for (var l = 0; l < arr.length; l++) {
                if (l != i && l != j && l != k) {
                  temp = [];
                  temp.push(arr[i]);
                  temp.push(arr[j]);
                  temp.push(arr[k]);
                  temp.push(arr[l]);
                  result.push(temp);
                }
              }
            }
          }
        }
      }
    }

    return result;
  }
  return f(arr);
}

//call this function
console.log(getAllCombinations(["a", "b", "c", "d"]));

[["a"],["a","b"],["a","b","c"],["a","b","c","d"],["a","b","d"],["a","b","d","c"],["a","c"],["a","c","b"],["a","c","b","d"],["a","c","d"],["a","c","d","b"],["a","d"],["a","d","b"],["a","d","b","c"],["a","d","c"],["a","d","c","b"],["b"],["b","a"],["b","a","c"],["b","a","c","d"],["b","a","d"],["b","a","d","c"],["b","c"],["b","c","a"],["b","c","a","d"],["b","c","d"],["b","c","d","a"],["b","d"],["b","d","a"],["b","d","a","c"],["b","d","c"],["b","d","c","a"],["c"],["c","a"],["c","a","b"],["c","a","b","d"],["c","a","d"],["c","a","d","b"],["c","b"],["c","b","a"],["c","b","a","d"],["c","b","d"],["c","b","d","a"],["c","d"],["c","d","a"],["c","d","a","b"],["c","d","b"],["c","d","b","a"],["d"],["d","a"],["d","a","b"],["d","a","b","c"],["d","a","c"],["d","a","c","b"],["d","b"],["d","b","a"],["d","b","a","c"],["d","b","c"],["d","b","c","a"],["d","c"],["d","c","a"],["d","c","a","b"],["d","c","b"],["d","c","b","a"]]

A total of 64 combinations for a 4 length array.

The function works fine but I need to make this function recursive. The for loops have to be nested based on the length of the array and the push also increased per nested loop.

Really appreciate some advice.

like image 570
Jeetu Avatar asked Dec 18 '25 00:12

Jeetu


1 Answers

Finally made it recursive !! Tried to work down on the the original code posted above moving each loop functionality into simple functions.

function getAllCombinations(inputArray) {
  var resultArray = [];
  var combine = function() {
    for (var i in inputArray) {
      var temp = [];
      var tempResult = [];
      for (var j in arguments) {
        tempResult.push(inputArray[arguments[j]]);
        if (arguments[j] == i) {
          temp = false;
        } else if (temp) {
          temp.push(arguments[j]);
        }
      }
      if (temp) {
        temp.push(i);
        combine.apply(null, temp);
      }
    }
    if (tempResult.length > 0) {
      resultArray.push(tempResult);
    }
    return resultArray;
  };
  return combine();
}

See the older version here.

Result produces 64 unique combinations for a 4 dimensional array

console.log(getAllCombinations(["a", "b", "c", "d"]));

[["a","b","c","d"],["a","b","c"],["a","b","d","c"],["a","b","d"],["a","b"],["a","c","b","d"],["a","c","b"],["a","c","d","b"],["a","c","d"],["a","c"],["a","d","b","c"],["a","d","b"],["a","d","c","b"],["a","d","c"],["a","d"],["a"],["b","a","c","d"],["b","a","c"],["b","a","d","c"],["b","a","d"],["b","a"],["b","c","a","d"],["b","c","a"],["b","c","d","a"],["b","c","d"],["b","c"],["b","d","a","c"],["b","d","a"],["b","d","c","a"],["b","d","c"],["b","d"],["b"],["c","a","b","d"],["c","a","b"],["c","a","d","b"],["c","a","d"],["c","a"],["c","b","a","d"],["c","b","a"],["c","b","d","a"],["c","b","d"],["c","b"],["c","d","a","b"],["c","d","a"],["c","d","b","a"],["c","d","b"],["c","d"],["c"],["d","a","b","c"],["d","a","b"],["d","a","c","b"],["d","a","c"],["d","a"],["d","b","a","c"],["d","b","a"],["d","b","c","a"],["d","b","c"],["d","b"],["d","c","a","b"],["d","c","a"],["d","c","b","a"],["d","c","b"],["d","c"],["d"]]
like image 77
Jeetu Avatar answered Dec 21 '25 02:12

Jeetu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!