Algorithm to find if two sets intersect

后端 未结 7 1692
一向
一向 2021-01-01 23:22

Let\'s say I have two arrays:

int ArrayA[] = {5, 17, 150, 230, 285};

int ArrayB[] = {7, 11, 57, 110, 230, 250};

Both arrays a

7条回答
  •  -上瘾入骨i
    2021-01-01 23:55

    If you are using C# 3.0 then why not take advantage of LINQ here?

    ArrayA.Intersect(ArrayB).Any()
    

    Not only is this generic (works for any comparable type) the implementation under the hood is pretty efficient (uses a hashing algorithm).

提交回复
热议问题