Reputation:
Hope someone can help me. I am using c# and I am somewhat new to it.
I am loading a text file into my app and splitting the data on "," I am reading part of string into a <list>
when the data is read there is a lot of duplicates that vary depending on the txt file that I load. Can someone tell me how to check the and remove any and all duplicates that come up. There is no way of knowing what duplicates will show up as there is infinite possibilities that it could be.
Thanks for the help in advance
Upvotes: 3
Views: 5145
Reputation: 74842
If you are targeting .NET 3.5, use the Distinct extension method:
var deduplicated = list.Distinct();
Upvotes: 7
Reputation: 31878
A simple/dirty example follows:
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;
}
As a warning: String.Split on very large strings can cause consume huge amounts of memory and cause exceptions.
Upvotes: 2
Reputation: 56123
If you load the strings into a Set
instead of into a List
then duplicates are discarded automatically.
Upvotes: 4