How to remove duplicates from a StringCollection in c#? I was looking for a more efficient approach. StringCollection is returned from an API.
-
4A more efficient approach than what?Dan Puzey– Dan Puzey2010-04-15 12:32:34 +00:00Commented Apr 15, 2010 at 12:32
-
Actually what I had in mind was to traverse every entity and remove all instances except one. But now I have found one statement solution.Tasawer Khan– Tasawer Khan2010-04-16 04:41:33 +00:00Commented Apr 16, 2010 at 4:41
4 Answers
Just use a HashSet<string> as your collection, rather than StringCollection. It is designed to prevent the addition of duplicate elements by comparing hash codes of those elements (thus being very efficient).
Edit: Since it would seem you're returned a StringCollection in the first place, then the solution should just be to loop over all the items in the StringCollection and add them to a HashSet<string>, thereby eliminating duplicates. The Enumerable.Distinct extension method would also do the job, but less efficiently I suspect, since it does use hashing (rather just normal equality testing). Something like this:
var noDuplicatesItems = stringCollection.Cast<string>().Distinct().ToArray();
13 Comments
HashSet<T> also.StringCollection doesn't appear to support IEnumerable<string>, so Enumerable.Distinct() wouldn't be available? StringCollection s = new StringCollection();
s.Add("s");
s.Add("s");
s.Add("t");
var uniques = s.Cast<IEnumerable>();
var unique = uniques.Distinct();
foreach (var x in unique)
{
Console.WriteLine(x);
}
Console.WriteLine("Done");
Console.Read();
Not tested for efficiency.
using linq: myCollection.Cast<string>.Distinct().ToList();
or you can use a HashSet as Noldorin proposed
1 Comment
Cast<string> will convert it into a generic IEnumerable<string> however, then you can use whatever you wish.