Intersection of two arrays in C#

CsharpProgrammingServer Side Programming

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
raja
Published on 09-Aug-2018 14:44:04
Advertisements