Get unique values from a List of strings
I have a List of strings like this:
{"100", "101, "101", "102, "103, "103", "104", "104", "105"}
And I need get a new list of strings with only the different values:
开发者_开发知识库{"100","101","102","103","104","105"}
Anyone have a quick way to do this?
You can use the Distinct method:
List<string> distinctList = dupeList.Distinct().ToList();
List<String> strings = new List<string>() { "100", "101", "101", "102", "103", "103", "104", "104", "105" };
var distinctStrings = strings.Distinct().ToList();
List<string> dupes = new List<string>(){"100", "101, "101", "102, "103, "103", "104", "104", "105"};
List<string> no_dupes = dupes.Distinct().ToList();
Or you could use a HashSet
var noDupes = new HashSet<string>(dupes).ToList();
Also see Remove Duplicates from a List in C#
精彩评论