Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove identical items from List<string>?

Tags:

c#

asp.net

I have a List in which I select users from db each time a sql query runs with certain value and selects one user in the time thus I cannot limit identical users in sql.

I have list with:

list[0] = "jerry"
list[1] = "tom"
list[2] = "jerry"

I want any (first or last doesn't matter in my case) to be removed from the list.

Thanks

like image 918
eugeneK Avatar asked Nov 27 '22 22:11

eugeneK


1 Answers

IEnumerable<string> uniqueUsers = list.Distinct();

You can also use a HashSet:

HashSet<string> uniqueUsers = new HashSet<string>(list);
like image 122
Lee Avatar answered Nov 29 '22 11:11

Lee