1

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?

guntbert
  • 536
  • 6
  • 19
ale
  • 3,301
  • 10
  • 40
  • 48
  • possible duplicate of this question: http://stackoverflow.com/questions/292307/selecting-unique-elements-from-a-list-in-c – yas4891 May 24 '11 at 17:03

3 Answers3

5

You can use the Distinct method:

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

FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55
3
List<String> strings = new List<string>() { "100", "101", "101", "102", "103", "103", "104", "104", "105" };
var distinctStrings = strings.Distinct().ToList(); 
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Alireza Maddah
  • 5,718
  • 2
  • 21
  • 25
0
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#

Community
  • 1
  • 1
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184