Efficient list of unique strings C#

If you are using .NET 3.5, the HashSet should work for you.

The HashSet<(Of <(T>)>) class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.


You can look to do something like this

var hash = new HashSet<string>();
var collectionWithDup = new []{"one","one","two","one","two","zero"}; 

// No need to check for duplicates as the Add method
// will only add it if it doesn't exist already
foreach (var str in collectionWithDup)
    hash.Add(str);   

I'm not sure if this counts as a good answer, but when faced with the need for a unique set that maintains insertion order, I compromised with a HashSet and a List side-by-side. In this case, whenever you add to the set, do the following:

if(hashSet.Add(item))
    orderList.Add(item);

When removing items, make sure to remove them from both. Thus, as long as you can be sure that nothing else added items to the list, you'll have an insertion-ordered unique set!


You could also use Linq as in:

using System.Linq;

var items = new List<string>() { "one", "one", "two", "one", "two", "zero" };

List<string> distinctItems = items.Distinct().ToList();