-3

I have multiple lists like so (Can be in any length and number):

['a1', 'a2']
['b1', 'b2', 'b3']

I can't figure out how to get all the possible combinations of these lists, wanted result:

[
  ['a1', 'b1'],
  ['a1', 'b2'],
  ['a1', 'b3'],
  ['a2', 'b1'],
  ['a2', 'b2'],
  ['a2', 'b3']
]

Does anyone have a suggestion for this (any language is fine)? I tried it using recursion but with no luck.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Two Horses
  • 1,401
  • 3
  • 14
  • 35

1 Answers1

-3

If I understand you correctly ( I'm not sure) but in java you should use Set to eliminate duplicates, then:

    List<String> list = Arrays.asList("a1", "a2");

    List<String> anotherList = Arrays.asList("b1", "b2", "b3");

    Set<String> set = new HashSet<>(list);
    Set<String> anotherSet = new HashSet<>(anotherList);

    set.stream()
        .forEach(setElement -> anotherSet.stream()
            .forEach(anotherSetElement -> System.out.println(setElement + " " + anotherSetElement)));
tomeszmh
  • 218
  • 2
  • 9