1

How to remove duplicates from a StringCollection in c#? I was looking for a more efficient approach. StringCollection is returned from an API.

2
  • 4
    A more efficient approach than what? Commented 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. Commented Apr 16, 2010 at 4:41

4 Answers 4

10

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();
Sign up to request clarification or add additional context in comments.

13 Comments

The OP did say the StringCollection was coming from an API.
@Rowland: Thanks, but really worth the down-vote? My answer still largely applies.
Good solution but... he could use it only with the framework 3.5 or above.
@Claudio: Yes, true... however with .NET 4.0 out now, it is likely most people are at least using .NET 3.5 for new programs. It wouldn't be too hard to implement your own HashSet<T> also.
@Noldorin StringCollection doesn't appear to support IEnumerable<string>, so Enumerable.Distinct() wouldn't be available?
|
1
    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.

2 Comments

Does the job, though I'm not a fan of the lack of generics here.
Try compiling it and running it.
1

If you're in v3.5 of the Framework (or later), then you can first convert to an IEnumerable<string>, and then call the Distinct() method on that; ie:

// where foo is your .Collections.Specialized.StringCollection
IEnumerable<string> distinctList = foo.OfType<string>.Distinct()

Comments

0

using linq: myCollection.Cast<string>.Distinct().ToList(); or you can use a HashSet as Noldorin proposed

1 Comment

No, because it's not generic. A simple Cast<string> will convert it into a generic IEnumerable<string> however, then you can use whatever you wish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.