C# List<T>的并集、交集、差集

集合的并集是合并集合的项,如下图所示:

List<int> ls1 = new List<int>() { 1,2,3,5,7,9 };
List<int> ls2 = new List<int>() { 2,4,6,8,9,10};

IEnumerable<int> unionLs = ls1.Union(ls2);
foreach (int item in unionLs)
{
    Console.Write("{0}\t", item);
}

集合的交集是取集合的共同的项,如下图所示:

 

List<int> ls1 = new List<int>() { 1,2,3,5,7,9 };
List<int> ls2 = new List<int>() { 2,4,6,8,9,10};


IEnumerable<int> intersectLs = ls1.Intersect(ls2);
foreach (int item in intersectLs)
{
    Console.Write("{0}\t",item);
}

集合的差集是取在该集合中而不在另一集合中的所有的项,如下图所示:

 

List<int> ls1 = new List<int>() { 1,2,3,5,7,9 };
List<int> ls2 = new List<int>() { 2,4,6,8,9,10};


IEnumerable<int> exceptLs = ls1.Except(ls2);
foreach (int item in exceptLs)
{
    Console.Write("{0}\t", item);
}

 

转载于:https://www.cnblogs.com/godbell/p/7535637.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值