最快的方法,以消除从列表&LT重复值;&GT;由拉姆达 [英] Fastest way to Remove Duplicate Value from a list<> by lambda
问题描述
什么是从列表中删除重复值最快的方法。
假设列表与LT;长&GT;多头=新的List&LT;长&GT; {1,2,3,4,3,2,5};
所以我在使用拉姆达有趣的删除重复并返回: {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.
请注意,<一个href=\"http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct.aspx\"><$c$c>Distinct()$c$c>不是的保证的以preserve原来的秩序,但在当前的实现它会 - 这就是最自然的实施。看到我的<一个href=\"http://$c$cblog.jonskeet.uk/2010/12/30/reimplementing-linq-to-objects-part-14-distinct.aspx\">Edulinq博客文章鲜明的()
了解详情。
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.
如果你不需要它是一个列表与LT;长&GT;
,你可以只把它作为:
If you don't need it to be a List<long>
, you could just keep it as:
IEnumerable<long> unique = longs.Distinct();
在这一点上,它会经过去欺骗你迭代每次独特
虽然。不管是好还是不好,将取决于您的要求。
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屋!