Fast Filter of Array With Respect to Another

11 Views Asked by At

Suppose I have an array of indexes, A. Suppose I have an array B, where every key is an array containing some indexes or number. I would know which of the entries in B contain some index appearing in A. For example (in php style):

A = [3,45,67,8]
B = [ 1 => [1,6,8],
      2 => [5,67,3,4,5,66,6],
      3 => [55,56,57,58],
      4 => [45,80,81,82]
    ]

The entries of B which contain some value of A are 2 and 4. So the function I would make should be:

function solution = filter(A,B) // solution = [2,4]

Now, with a brute loop, looping over entries of B, the complexity will be O(nm), where n is #B and m is the size of longest row in B. There are smarter solutions?