C# 程式列印兩個列表中所有公共元素
首先,建立兩個列表 -
List list1 = new List() {40, 20, 60, 3, 55};
List list2 = new List() {20, 70, 55, 80};如需查詢公共元素,請使用交集 -
list1.Intersect(list2)
以下是查詢兩個列表之間公共元素的完整程式碼 -
示例
using System;
using System.Linq;
using System.Collections.Generic;
namespace Demo {
class Program {
static void Main(string[] args) {
List list1 = new List() {40, 20, 60, 3, 55};
List list2 = new List() {20, 70, 55, 80};
Console.WriteLine("Common elements:");
foreach(int value in list1.Intersect(list2))
Console.WriteLine(value);
}
}
}輸出
Common elements: 20 55
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP