5

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
undefined
  • 33,537
  • 22
  • 129
  • 198
  • Is your list sorted? If not, is sorting your list once before you check an option? – Szymon Mar 18 '14 at 00:59
  • @Szymon I could sort it, but it would still be slower than using a dictionary. I think binary search is O(log(n)) which is bigger than O(1) for hash table lookups – undefined Mar 18 '14 at 01:03
  • Just as a note O(1) doesn't mean that it's faster necessarily, only that the access to each item is constant. – kemiller2002 Mar 18 '14 at 01:05
  • @kevin you're absolutely right (eg small collections) however for my application I'm normally dealing with large sets – undefined Mar 18 '14 at 01:07
  • Duplicate: http://stackoverflow.com/questions/20482848/best-lookup-data-structure-to-store-only-the-keys-dictionary-without-value you can even search google before asking in SO – thepirat000 Mar 18 '14 at 01:07

1 Answers1

11

HashSet has a method Contains which is an O(1) search of the values. This should suffice (assuming your TKey implements GetHashCode correctly) :)

Xenolightning
  • 4,140
  • 1
  • 26
  • 34
  • This is way better, exactly what im looking for. Thanks – undefined Mar 18 '14 at 01:04
  • 1
    @Xenolightning: Good answer. If I may add a detail: AFAIK, `TKey` needs to implement both `GetHashCode` *and* `Equals` correctly, not just the former. (`GetHashCode` by itself only suffices to determine inequality of two objects, but when two objects have the same hash code, that doesn't always mean that they're equal. In that case, only a full equality comparison via `Equals` will tell.) – stakx - no longer contributing Apr 16 '17 at 08:58
  • @stakx Yeah, you're absolutely right. `GetHashCode` is used for the initial look at where the object should be, `Equals` is then used if something exists in it's slot. Thanks for that clarification. – Xenolightning Apr 17 '17 at 20:49