C# Remove Duplicates in List

有好多个方法实现

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

 

 

posted @ 2013-02-01 16:31  太古月石  阅读(359)  评论(0编辑  收藏  举报