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


Gitu

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 of 4 length arrays.

The function works fine, but I need to make the function recursive. The for loops must be nested based on the length of the array, and the push for each nested loop will also increase.

Really appreciate some suggestions.

Gitu

Finally made it recursive! Tried to work on the original code posted above, converting each loop function into a simple function.

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();
}

Check out the old version here .

The result generates 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"]]

Related


How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations from an array?

username I have an array of Types and I want to generate all possible combinations for these types. ie: (assumes only int, bool and string) int/bool/string/int, bool/int, string/int, bool, string/int, string, bool/etc I saw Eric Lippert's answer here: Generate

How to get all combinations of array elements in Ruby?

Michael Durant If I have an array: %w(a b c d e) => ["a","b","c","d","e"] I can get some combinations irb(main):071:0> %w(a b c d e).combination(3).to_a => [["a", "b", "c"], ["a", "b", "d"], ["a", "b", "e"], ["a", "c", "d"], ["a", "c", "e"], ["a", "d", "e"],

How to get all combinations of array elements in Ruby?

Michael Durant If I have an array: %w(a b c d e) => ["a","b","c","d","e"] I can get some combinations irb(main):071:0> %w(a b c d e).combination(3).to_a => [["a", "b", "c"], ["a", "b", "d"], ["a", "b", "e"], ["a", "c", "d"], ["a", "c", "e"], ["a", "d", "e"],

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations in every possible order?

Miklath Stack Overflow Community! I am asking for your help again... I need to make a combination generator to get all possible combinations of a list. Before you tell me to use itertools, let me tell you why it doesn't work in my case: My code looks like this