C# 中的兩個陣列的交集
要獲取兩個陣列的交集,請使用 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP