What data structures in .NET do O (1) do in Contains () calls?

I draw a space here; I can’t find him unless I really notice something under my nose.

I am trying to save an int list in a data structure.
But after I add them, I will check the code later if an int already exists in the list.

A generic List<int> performs an O (n) operation with its Contains() .
I want something that works as fast as Dictionary<> Contains() , which does the O (1) operation because it hashes keys.

I know that the answer is so simple and that I worked too long today, I can’t remember it.

Help!

+6
source share
2 answers

Will a HashSet<T> ?

+9
source share

Since I work with C # 2.0 due to platform limitations, I usually use Dictionary<int,bool> , with the restriction that if the key is on the map, bool is true (so bool does not encode any information) it's just a replacement missing element type from .NET).

+1
source share

All Articles