I have few arrays of integers. Elements in each array are ordered. Arrays does not have duplicates.
I need to join all arrays into one so the resulting array contains only elements which exist in every array.
For example, I have arrays
(1,2,3,4,5)
(2,3,5)
(1,2,4,5)
The result has to be (2,5)
What is the best way to do it to achieve best performance?
If the arrays are expected to contain many different numbers and only few present in all of them,
length(intersection)*log(length(array)) >= length(array), and looking up the elements of the intersection inarrayotherwise.Worst case complexity is O(sum(lengths)), if you’re lucky, you get around
k * sum(log(length)), wherekis the number of elements in the intersection.