Haim Evgi
Haim Evgi

Reputation: 125496

Intersect two arrays

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

Upvotes: 20

Views: 31771

Answers (2)

HasanG
HasanG

Reputation: 13161

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";
}

Upvotes: 15

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

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

Upvotes: 25

Related Questions