获取唯一值

问题描述:

我有一个字符串像这样的列表:获取唯一值

{"100", "101, "101", "102, "103, "103", "104", "104", "105"}

我需要得到一个新的字符串列表,只有不同的值:

{"100","101","102","103","104","105"}

任何人都有一个快速的方法来做到这一点?

+0

可能DUPLI这个问题的解答: http://stackoverflow.com/questions/292307/selecting-unique-elements-from-a-list-in-c – yas4891 2011-05-24 17:03:51

List<String> strings = new List<string>() { "100", "101", "101", "102", "103", "103", "104", "104", "105" }; 
var distinctStrings = strings.Distinct().ToList(); 

您可以使用DISTINCT方法:

List<string> distinctList = dupeList.Distinct().ToList();

List<string> dupes = new List<string>(){"100", "101, "101", "102, "103, "103", "104", "104", "105"}; 
List<string> no_dupes = dupes.Distinct().ToList(); 

或者你可以使用一个HashSet

var noDupes = new HashSet<string>(dupes).ToList(); 

另见Remove Duplicates from a List in C#