3

I have an array (let's call it masterArray) that contains n inner arrays (number of arrays can be infinite): innerArray1, innerArray2, innerArray3. How to get common elements of all arrays? By common elements I mean elements that exists in every inner array.

Below is my array structure:

masterArray = [
    innerArray1 = [
        element1,
        element2,
        element5,
        element6,
        element8
    ],
    innerArray2 = [
        element1,
        element2,
        element4,
        element5,
        element10
    ],
    innerArray3 = [
        element2,
        element1,
        element5,
        element3,
        element10
    ]
]

So common elements of these three inner arrays would be only: element1, element2 and element5.

Paweł
  • 1,286
  • 1
  • 14
  • 17
  • @blgt not really, in my case there's more than 2 arrays (infinite number of arrays) and they are not sorted actually. – Paweł Oct 23 '14 at 14:31
  • 2
    "number of arrays can be infinite" – It most certainly cannot. Mabye you mean arbitrary, but surely not infinite. – Ingo Bürk Oct 23 '14 at 14:32
  • 2
    @PawełLudwiczak Your case is different in the same way multiplication is different to addition. Just stick it in a loop and you're done. – blgt Oct 23 '14 at 14:34
  • 1
    There's actually answer for what I've been looking for: http://stackoverflow.com/questions/11076067/finding-matches-between-multiple-javascript-arrays My question can be closed. – Paweł Oct 23 '14 at 14:45

0 Answers0