2

I had an array like this below

const myArray = [1, 2, 3, 4, 5, 6];

and I was able to get every possible combination's out of it with this function.

function getCombinations(list) {
  var set = [],
    listSize = list.length,
    combinationsCount = 1 << listSize,
    combination;

  for (var i = 1; i < combinationsCount; i++) {
    var combination = [];
    for (var j = 0; j < listSize; j++) {
      if (i & (1 << j)) {
        combination.push(list[j]);
      }
    }
    set.push(combination);
  }
  return set;
}

it works fine and I get 63 possible combination with the array above

my problem begins here i want every possible combination but with one exception, my array of items are in group like this below

const myArray = [
  { groupId: "foo", value: 1 },
  { groupId: "foo", value: 2 },
  { groupId: "foo", value: 3 },
  { groupId: "bar", value: 4 },
  { groupId: "bar", value: 5 },
  { groupId: "zoo", value: 6 },
];

I want to get every possible combination but each combination must only have one item of a group

for example there are correct out put I'm looking for

const myArray = [
  { groupId: "foo", value: 1 },
];

const myArray = [
  { groupId: "foo", value: 1 },
  { groupId: "zoo", value: 6 },
];

const myArray = [
  { groupId: "foo", value: 1 },
  { groupId: "bar", value: 5 },
  { groupId: "zoo", value: 6 },
];

const myArray = [
  { groupId: "bar", value: 5 },
  { groupId: "zoo", value: 6 },
];

As you can see I want to each group only have one of it items in the generated combinations

These are the combination that I don't want to be generated

const myArray = [
  { groupId: "bar", value: 4 },
  { groupId: "bar", value: 5 },
  { groupId: "zoo", value: 6 },
];

const myArray = [
  { groupId: "foo", value: 1 },
  { groupId: "zoo", value: 6 },
  { groupId: "zoo", value: 7 },
];

we have two item with zoo group tag I don't want this outputs.

sadpepe
  • 21
  • 1
  • Which one to keep when you have 2/3 same groupId. One with max value? or one which comes first or one which came latest – navnath Sep 12 '21 at 07:36
  • In your example : `const myArray = [ { groupId: "bar", value: 5 }, { groupId: "zoo", value: 6 }, ];` Why "foo" gropuId is removed? – navnath Sep 12 '21 at 07:37
  • @NavnathJadhav Thank you for your time, if a group have 3 item for example, then there should be a combination(s) where they are with other items in the array which is not the same groupId, so as long as they are not in the same combinations it's good. for the second question. i want every possible combination this includes one item alone for example `[foo]` when i say every possible combination i mean it, if you test the code i've provided you what is the output should look like – sadpepe Sep 12 '21 at 07:41
  • Split your input into separate arrays by group id and compute a cartesian product (set product) of these groups. – georg Sep 12 '21 at 07:55
  • @georg Can you please provide a working code? i really can't figure this out myself – sadpepe Sep 12 '21 at 08:01
  • grouping should be easy, for cartesian products see answers over here: https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript – georg Sep 12 '21 at 08:08
  • @georg cartesian products is definitely won't work in my situation since i need all the possible combinations i don't think that how `cartesian products` works, lets say i have this array `[1,2,3]` i need to get these combinations `[1], [2], [3], [1,2], [1,3], [1,2,3]` if i use cartesian products I do not get the desired output – sadpepe Sep 12 '21 at 08:59
  • @sadpepe: yes, you can added a dummy element to each group, compute the product and remove dummy elements from the results. – georg Sep 12 '21 at 10:25
  • @georg I will try that, do you have any idea how can I transform my current function to achieve the desired result? – sadpepe Sep 12 '21 at 11:19

0 Answers0