tags:

views:

72

answers:

3

I'm drawing a blank here; I can't find it, unless I'm really overlooking something under my nose.

I'm trying to store a list of ints in a data structure.
But after I add them, I will later in code check if an int exists in the list already.

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

I know the answer is something so simple and that I've worked for too long today I can't remember it.

Help!

+6  A: 

Will HashSet<T> work for you?

Daniel A. White
cannot be used if you really need to store a list, though (the order will be lost).
Thilo
Wow that simple...... and it was RIGHT in front of me the whole time. I wish I can answer this question faster =). Thanks!
Beemer
Oh I don't care about the order, Thilo. That's exactly what I want; just a bag of integers that I can index quickly.
Beemer
+1  A: 

Since I work with C# 2.0 because of platform constraints, I usually use Dictionary<int,bool>, with the constraint that if a key is in the map, the bool is true (so the bool isn't really encoding any information -- it's just a substitute for the missing unit type from .NET).

luqui
A: 

hashset, dictionary

saurabh