C#获取列表中的非重复项 [英] C# Get non duplicates in a list

查看:30
本文介绍了C#获取列表中的非重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带有这样的列表:

int[] numbers = {1,2,2,3,3,4,4,5};

我可以使用Distinct()函数删除重复项,因此列表将显示为:1,2,3,4,5

I can remove the duplicates using the Distinct() function so the list will read: 1,2,3,4,5

但是,我要反过来.我希望它删除所有重复的数字,让我留下唯一的数字.

however, I want the inverse. I want it to remove all of the numbers that are duplicated leaving me with the unique ones.

因此列表将显示为:1,5.

So the list will read: 1,5.

这将如何完成?

推荐答案

一种方法是

var singles = numbers.GroupBy(n => n)
                     .Where(g => g.Count() == 1)
                     .Select(g => g.Key); // add .ToArray() etc as required

这篇关于C#获取列表中的非重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆