1

I'm not sure how this operation is called so I failed to google it.

I have a multiple 1-dimensional arrays and want to construct and array that contains all possible combinations of values from each array. So for example if I have 3 arrays:

["a", "b"], ["z", "x"], ["1", "2"]

I want to get a result:

[["a", "z", "1"], 
 ["a", "z", "2"],
 ["a", "x", "1"],
 ["a", "x", "2"],
 ["b", "z", "1"], 
 ["b", "z", "2"],
 ["b", "x", "1"],
 ["b", "x", "2"]]

The only way I can think of is making a recursive function that will pick an element from 1 array for each call. But that seems unnecessarily complex to me. Maybe there is much simpler solution or built in function for that?

Poma
  • 8,174
  • 18
  • 82
  • 144
  • Possible duplicate of [Generating permutations of a set (most efficiently)](http://stackoverflow.com/questions/11208446/generating-permutations-of-a-set-most-efficiently) – Marko Gresak May 08 '16 at 00:08
  • I don't understand how the question you linked is related to my question – Poma May 08 '16 at 00:15
  • you want to generate permutations from these arrays. The link provides an answer on how to do that. Your case is a bit more specific, but it shouldn't be too difficult to adjust it to your needs. – Marko Gresak May 08 '16 at 00:19
  • [Looking at each combination in jagged array](http://stackoverflow.com/questions/35493785/looking-at-each-combination-in-jagged-array/35494340#35494340) – Ivan Stoev May 08 '16 at 00:38
  • 1
    I like recursive solutions. They are usually the simplest method and produce code the is understandable and easily maintainable. I'm not sure why the world things they are complex. Been writing recursive solutions for 40 years since Computer Science Class 201 in college. – jdweng May 08 '16 at 01:41

0 Answers0