开发者

Get unique values from a List of strings

开发者 https://www.devze.com 2023-03-08 06:18 出处:网络
I have a List of strings like this: {\"100\", \"101, \"101\", \"102, \"103, \"103\", \"104\", \"104\", \"105\"}

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#

0

精彩评论

暂无评论...
验证码 换一张
取 消