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

前端 未结 6 1251
南方客
南方客 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 07:55

    static void Main(string[] args)
    
    
        int[] arr1 = { 16, 48, 40, 32, 5, 7 };
        int[] arr2 = { 48, 32, 16, 40, 56, 72, 16, 16, 16, 16, 16, 5, 7, 6, 56 };
        int k = 0;
        for (int i = 0; i < arr1.Length; i++)
        {
    
            for (int j = 0; j < arr2.Length; j++)
            {
    
                if (arr1[i] == arr2[j])
                {
                    k++;
                    break;
                }
    
            }
    
        }
        if (arr1.Length == k)
        {
            Console.WriteLine(true);
        }
        else
            Console.WriteLine(false);
    }
    ----
    

提交回复
热议问题