C A way to compare two arrays and find the same or different elements

  • 2021-08-31 08:58:25
  • OfStack

This article illustrates how C # compares two arrays and finds the same or different elements. Share it for your reference, as follows:


string[] arr1 = new[] { "1", "2", "3", "4", "5" };
string[] arr2 = new[] { "1", "3", "5" };
var sameArr = arr1.Intersect(arr2).ToArray();
// Find the same element ( That is, intersection )
var diffArr = arr1.Where(c => !arr2.Contains(c)).ToArray();
// Find out the different elements ( That is, the complement set of intersection )

I hope this article is helpful to everyone's C # programming.


Related articles: