I often want to optimize the performance of a .Contains
on a collection.
I have always done this by creating a Dictionary<TKey,bool>
and then using .ContainsKey on the dictionary to give O(1) .Contains
performance.
However it always irks me that I actually don't care at all about the value in the dictionary.
Is there a better data structure than dictionary to support this case where I don't care about the actual value, only the existence of the key?
HashSet
has a method Contains
which is an O(1) search of the values. This should suffice (assuming your TKey
implements GetHashCode
correctly) :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With