Removing duplicates from a list collection

Hope someone can help me. I am using C # and I am a bit new to this. I upload a text file to my application and split the data into "," I read part of the line in <list>, when the data is read, there are many duplicates that vary depending on the txt file being uploaded. Can someone tell me how to check and delete all and all duplicates that appear. There is no way to know which duplicates will be displayed, as there are endless possibilities that this can be.

Thanks for the help in advance.

+3
source share
4 answers

If you are targeting .NET 3.5, use the Distinct extension method:

var deduplicated = list.Distinct();
+7

Set List, .

+4

/ :

public List<string> RemoveDuplicates(List<string> listWithDups)
{
   cleanList = new List<string>();
   foreach (string s in listWithDups)
   {
      if (!cleanList.Contains(s))
         cleanList.Add(s);
   }
   return cleanList;
}

: String.Split .

+2
+1

Source: https://habr.com/ru/post/1702787/


All Articles