HashSet<T> versus Dictionary<K, V> w.r.t searching time to find if an item exists

halivingston picture halivingston · Apr 28, 2010 · Viewed 71.5k times · Source
HashSet<T> t = new HashSet<T>();
// add 10 million items


Dictionary<K, V> t = new Dictionary<K, V>();
// add 10 million items.

Whose .Contains method will return quicker?

Just to clarify, my requirement is I have 10 million objects (well, strings really) that I need to check if they exist in the data structure. I will NEVER iterate.

Answer

had picture had · Apr 27, 2012

HashSet vs List vs Dictionary performance test, taken from here.

Add 1000000 objects (without checking duplicates)

Contains check for half the objects of a collection of 10000

Remove half the objects of a collection of 10000