您的位置:首页 > 编程语言 > C#

C# Remove Duplicates in List

2013-02-01 16:31 369 查看
有好多个方法实现

1. 用一个Dictionary实现。

参考代码

static List<string> removeDuplicates(List<string> inputList)
{
Dictionary<string, int> uniqueStore = new Dictionary<string, int>();
List<string> finalList = new List<string>();

foreach (string currValue in inputList)
{
if (!uniqueStore.ContainsKey(currValue))
{
uniqueStore.Add(currValue, 0);
finalList.Add(currValue);
}
}
return finalList;
}


2. 用LINQ的Distinct方法。

简单的List 直接用就可以了 http://www.dotnetperls.com/remove-duplicates-list
如果是要Distinct一个类,要 Custom Comparer , 方法嘛。看这里:
http://msdn.microsoft.com/en-us/library/bb338049.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: