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