HashSet<T>
suggest changeHashSet<T> is a collection of unique items, with average O(1) lookup. A pathological set of hashes can make a lookup scan many items.
HashSet<int> validStoryPointValues = new HashSet<int>() { 1, 2, 3, 5, 8, 13, 21 };
bool containsEight = validStoryPointValues.Contains(8); // O(1)
By way of comparison, doing a Contains on a List yields poorer performance:
List<int> validStoryPointValues = new List<int>() { 1, 2, 3, 5, 8, 13, 21 };
bool containsEight = validStoryPointValues.Contains(8); // O(n)
HashSet<T>.Contains uses a hash table, so a lookup stays fast as the collection grows, as long as hashes are well distributed. List<T>.Contains scans the list.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents