相交的兩個陣列
若要獲取兩個陣列的交集,請使用 Intersect 方法。該方法是 System.Linq 名稱空間的一個擴充套件方法。
該方法將返回兩個陣列之間的共有元素。
首先設定兩個陣列 −
int[] arr1 = { 44, 76, 98, 34 }; int[] arr2 = { 24, 98, 44, 55, 47, 86 };
現在對兩個陣列使用 Intersect −
Arr1.Intersect(arr2);
以下是完整程式碼 −
示例
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); } } }
輸出
44 98
廣告