0

I have a multi-dimensional array storing groups of N as below. From each group I need to get a distinct N-tuple, as shown in the example. I may not know in advance the size of the group.

I started creating nested for-loops (with the intention to add it to a Set to avoid duplicates) but somehow I'm drawing a blank on this one. Is there a scalable algorithm, or a JS Set I can use here? Would JS even allow you to implement a custom Object Set with its own equals() like Java?

I need to solve this problem in JavaScript, but in theory it's an algorithm question.

var array = [
              [
                ["abc", "def"],
                ["123", "456", "789"],
                ["-", "--"]
              ],
              
              [
                ["X"], 
                ["0", "1", "2"]
              ]
            ];
            
 /* Expected result after processing:
 
    ["abc", "123", "-"],
    ["abc", "123", "--"],
    ["abc", "456", "-"],
    ["abc", "456", "--"],
    ["abc", "789", "-"],
    ["abc", "789", "--"],
    ["def", "123", "-"],
    ["def", "123", "--"],
    ["def", "456", "-"],
    ["def", "456", "--"],
    ["def", "789", "-"],
    ["def", "789", "--"],
    // Next element
    ["X", "0"],
    ["X", "1"],
    ["X", "2"]
*/

for (var i = 0; i < array.length; i++) {
  for (var j = 0; j < array[i].length; j++) {
    for (var k = 0; k < array[i][j].length; k++) {  // May not exist
       // ??
    }
  }
}

console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
gene b.
  • 10,512
  • 21
  • 115
  • 227

0 Answers0