18

How can I find the intersecttion between 2 arrays in C#, in a fast way?

istepaniuk
  • 4,016
  • 2
  • 32
  • 60
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223

2 Answers2

23

There's the Intersect extension method on Enumerable. It works on any IEnumerable<T> including arrays.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
12

Here is an example use of Linq Intersect.

// Assign two arrays.
int[] array1 = { 1, 2, 4 };
int[] array2 = { 2, 3, 4 };

// Call Intersect extension method.
var intersect = array1.Intersect(array2);

foreach (int value in intersect)
{
    label1.Text += value + "\n";
}
HasanG
  • 12,734
  • 29
  • 100
  • 154