C#中确定重复的列表 [英] C# Determine Duplicate in List
本文介绍了C#中确定重复的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要求:在一个无序列表,确定是否存在重复。典型的方式,我会做,这是一个n平方嵌套循环。我不知道别人是怎么解决这个问题。是否有LINQ的优雅,高性能的方法?通用的东西是需要一个lambda或比较器将是很好。
Requirement: In an unsorted List, determine if a duplicate exists. The typical way I would do this is an n-squared nested loop. I'm wondering how others solve this. Is there an elegant, high performance method in Linq? Something generic that takes a lambda or a comparer would be nice.
推荐答案
除非我失去了一些东西,那么你应该能够逃脱使用一些简单的鲜明的()
。如果任何重复得到去除当然也不会是最复杂的实现,你可以想出,但它会告诉你:
Unless I'm missing something, then you should be able to get away with something simple using Distinct()
. Granted it won't be the most complex implementation you could come up with, but it will tell you if any duplicates get removed:
var list = new List<string>();
// Fill the list
if(list.Count != list.Distinct().Count())
{
// Duplicates exist
}
这篇关于C#中确定重复的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文