How to check if an array contains any item of another array

前端 未结 6 1257
南方客
南方客 2021-01-31 07:35

Given 2 int arrays e.g, foo and bar, what\'s the most efficient way to check that the array bar contains at least one item that foo contains. should re

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 08:09

    Yes nested loops, although one is hidden:

    bool AnyAny(int[] A, int[]B)
    {
        foreach(int i in A)
           if (B.Any(b=> b == i))
               return true;
        return false;
    }
    

提交回复
热议问题