0

I need to find the values in array A that don't exist in array B

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

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

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

Can anyone give me any pointers?

MattDavey
  • 8,897
  • 3
  • 31
  • 54
user886550
  • 211
  • 2
  • 9

3 Answers3

14

Using LINQ:

  var C = A.Except(B);

or, if you want it as an array:

  int[] C = A.Except(B).ToArray();
H H
  • 263,252
  • 30
  • 330
  • 514
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);
MattDavey
  • 8,897
  • 3
  • 31
  • 54
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); 
    } 
}
Jesse Seger
  • 951
  • 1
  • 13
  • 31