Javascript find all combinations in order for numbers in array -


assume i've got array:

var arr = [1, 1, 2, 3, 3]; 

i need compute unique possible combinations in order out of array use numbers possible, provided each unique. don't care values instead matrix of indices match:

[0, 2, 3] [0, 2, 4] [1, 2, 3] [1, 2, 4] 

i looked @ this answer, when that:

var arr = [1, 1, 2, 3, 3]; console.log(allpossiblecases(arr)); 

i get:

["1 1 2 3 3"] 

which make sense according function does, i've been unable tweak enough suit want.

edit:

note part said in order. not complete set of permutations , none of answers in this question i'm asking.

for using function this answer need bit prepare array array.reduce this

var prepared = [1, 1, 2, 3, 3].reduce(function(acc,el,index){     var cur = acc.map[el];      if(!cur) {          acc.map[el] = cur = [];          acc.res.push(cur);      }      cur.push(index);      return acc;  },{res:[],map:{}}).res; 

and prepared looks [[0,1],[2],[3,4]]

here group index duplicated items.


Comments