1
var combiArray =  ["a", "b", "c"];

var result = [];

for(var i =0 ; i<combiArray.length;i++){
    result.push(combiArray[i])
    for(var b =0 ; b<combiArray.length;b++){
        if(i!=b){
            result.push(combiArray[i]+" "+combiArray[b])
        }

    }
}

//MY OUTPUT: 
[ 'a', 'a b', 'a c', 'b', 'b a', 'b c', 'c', 'c a', 'c b' ]

//WHAT I WANT IS THIS SEQUENCE
[
'a',
'a b',
'a b c',
'a c',
'a c b',
'b',
'b a',    
'b a c',
'b c',    
'b c a',    
'c',
'c a',
'c a b',
'c b',
'c b a',
]
Tom O.
  • 5,730
  • 2
  • 21
  • 35
Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67

1 Answers1

2

What you need is permutations for all the combinations

so here is a code for creating all the combinations:

function combination(arr) {
let res= [], len = Math.pow(2, arr.length), c, b, com;
    for(c=1;c<len;c++) {
        b = c.toString(2).padStart(arr.length, "0").split("");
        com = arr.filter((_, idx)=>b[idx]==="1");
        res.push(com.join(" "));
    }
    return res;
    }

In action

function combination(arr) {
let res= [], len = Math.pow(2, arr.length), c, b, com;
for(c=1;c<len;c++) {
 b = c.toString(2).padStart(arr.length, "0").split("");
 com = arr.filter((_, idx)=>b[idx]==="1");
 res.push(com.join(" "));
}
return res;
}

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

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

Now, you need permutation for each of the combination. for example, for a particular combination a b c you need to find ["abc", "cba", "acb", ....].

So let's create a code for permutation:

This code of permutation is taken from this stackoverflow question

function permutation(input) {
var permArr = [],
  usedChars = [];

function permute(input) {
  var i, ch;
  for (i = 0; i < input.length; i++) {
    ch = input.splice(i, 1)[0];
    usedChars.push(ch);
    if (input.length == 0) {
      permArr.push(usedChars.slice());
    }
    permute(input);
    input.splice(i, 0, ch);
    usedChars.pop();
  }
  return permArr
}

return permute(input);
}

In Action:

function permutation (input) {
    var permArr = [],
      usedChars = [];
    
    function permute(input) {
      var i, ch;
      for (i = 0; i < input.length; i++) {
        ch = input.splice(i, 1)[0];
        usedChars.push(ch);
        if (input.length == 0) {
          permArr.push(usedChars.slice());
        }
        permute(input);
        input.splice(i, 0, ch);
        usedChars.pop();
      }
      return permArr
    }
    
    return permute(input);
    }
    
    console.log(permutation (["a", "b"]));
    console.log(permutation (["a", "b", "c"]));

Now You need to combine them in order to get the desire output to have permutation of all the combination. And then we can look into the sequence once we have all the outputs we want. a sample sorting is added in this example below.

function permutation(input) {
  var permArr = [],
    usedChars = [];

  function permute(input) {
    var i, ch;
    for (i = 0; i < input.length; i++) {
      ch = input.splice(i, 1)[0];
      usedChars.push(ch);
      if (input.length == 0) {
        permArr.push(usedChars.slice());
      }
      permute(input);
      input.splice(i, 0, ch);
      usedChars.pop();
    }
    return permArr
  }

  return permute(input);
}

function allCombos(arr) {
let res= [], len = Math.pow(2, arr.length), c, b, com, per;

for(c=1;c<len;c++) {
 b = c.toString(2).padStart(arr.length, "0").split("");
 com = arr.filter((_, idx)=>b[idx]==="1");
 per = permutation(com).map(e=>e.join(" "));
 res.push(...per);
}
return res;
}

var res = allCombos(["a", "b", "c"]);

console.log(res);

//Now, we get all the outputs we want. for the sequence it's seems like its
//an alphabetical sequence, or its a sequencne in order they appears in the input collection.
//Let's just sort alphabetically, if any other sort is required that can be easily done in the final output.
var finalRes = res.sort((a,b)=> a.localeCompare(b));
console.log('Final resule: ', finalRes);
Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32