从列表中删除重复值的最快方法<>由拉姆达 [英] Fastest way to Remove Duplicate Value from a list<> by lambda

查看:12
本文介绍了从列表中删除重复值的最快方法<>由拉姆达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从列表中删除重复值的最快方法是什么.假设 List;longs = 新列表{ 1, 2, 3, 4, 3, 2, 5 }; 所以我很感兴趣使用 lambda 来删除重复项并返回:{1, 2, 3, 4, 5}.您有什么建议?

what is fastest way to remove duplicate values from a list. Assume List<long> longs = new List<long> { 1, 2, 3, 4, 3, 2, 5 }; So I am interesting in use lambda to remove duplicate and returned : {1, 2, 3, 4, 5}. What is your suggestion?

推荐答案

获得列表的最简单方法是:

The easiest way to get a new list would be:

List<long> unique = longs.Distinct().ToList();

这对你来说足够好,还是你需要改变现有列表?后者明显更冗长.

Is that good enough for you, or do you need to mutate the existing list? The latter is significantly more long-winded.

注意Distinct()保证保留原始顺序,但在当前的实现中它会 - 这是最自然的实现.请参阅我的 Edulinq 博客文章关于 Distinct() 了解更多信息.

Note that Distinct() isn't guaranteed to preserve the original order, but in the current implementation it will - and that's the most natural implementation. See my Edulinq blog post about Distinct() for more information.

如果你不需要它是一个List,你可以把它保持为:

If you don't need it to be a List<long>, you could just keep it as:

IEnumerable<long> unique = longs.Distinct();

此时它会在每次迭代 unique 时进行重复数据删除.这是否好取决于您的要求.

At this point it will go through the de-duping each time you iterate over unique though. Whether that's good or not will depend on your requirements.

这篇关于从列表中删除重复值的最快方法&lt;&gt;由拉姆达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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