找到另一个数组中不存在的数字

时间:2012-02-02 18:15:40

标签: c# linq

我需要找到数组A中不存在于数组B中的值

A = {1,2,3,4,5,6,7,8,9,10}

B = {1,2,3,7,8}

result = {4,5,6,9,10}

任何人都可以给我任何指示吗?

3 个答案:

答案 0 :(得分:14)

使用LINQ:

  var C = A.Except(B);

或者,如果你想要它作为一个数组:

  int[] C = A.Except(B).ToArray();

答案 1 :(得分:5)

var arrayA = new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var arrayB = new [] { 1, 2, 3, 7, 8 };

var result = arrayA.Except(arrayB);

答案 2 :(得分:1)

public void Linq52() 
{ 
    int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 
    int[] numbersB = { 1, 3, 5, 7, 8 }; 

    IEnumerable<int> aOnlyNumbers = numbersA.Except(numbersB); 

    Console.WriteLine("Numbers in first array but not second array:"); 
    foreach (var n in aOnlyNumbers) 
    { 
        Console.WriteLine(n); 
    } 
}