Intersection of two arrays in C#


To get intersection of two arrays, use the Intersect method. It is an extension method from the System.Linq namespace.

The method returns the common elements between the two arrays.

Set the two arrays first −

int[] arr1 = { 44, 76, 98, 34 };
int[] arr2 = { 24, 98, 44, 55, 47, 86 };

Now use the Intersect on both the arrays −

Arr1.Intersect(arr2);

The following is the complete code −

Example

 Live Demo

using System;
using System.Linq;

class Program {
   static void Main() {
      int[] arr1 = { 44, 76, 98, 34 };
      int[] arr2 = { 24, 98, 44, 55, 47, 86 };
      var intersect = arr1.Intersect(arr2);
      foreach (int res in intersect) {
         Console.WriteLine(res);
      }
   }
}

Output

44
98

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements