Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a class like Dictionary<> in C#, but for just keys, no values?

I guess another way to phrase this would be "Is there a class like List<> in C#, but optimized for checking whether a particular value is present?" I'm sure for a small set of values List<>.Contains would probably be fine, but what if I have a set of thousands or millions of values and wanted to find out whether a certain value was in it?

I've implemented this kind of thing in the past by creating a Dictionary<object, int> and setting the value to 0 for every key, but this feels really clunky. And now there's Stack Overflow, where my stupid question can be transformed into education for thousands (dozens, even). So here it is!

I'm not even sure what such a class would be called, other than maybe Set, so obviously searches on the topic have been... challenging :)

like image 308
Chuck Wilbur Avatar asked Mar 06 '10 00:03

Chuck Wilbur


People also ask

Is there a dictionary in C?

Generally, the C standard library does not include a built-in dictionary data structure, but the POSIX standard specifies hash table management routines that can be utilized to implement dictionary functionality.

What is dictionary class in C#?

The Dictionary<TKey, TValue> Class in C# is a collection of Keys and Values. It is a generic collection class in the System. Collections. Generic namespace. The Dictionary <TKey, TValue> generic class provides a mapping from a set of keys to a set of values.

Can a dictionary have two keys in C#?

It's a dictionary of dictionaries, so you have 2 keys to access each object, the key for the main dictionary to get you the required sub dictionary, and then the second key for the sub dictionary to get you the required item.

How do you check if a key is in a dictionary C#?

Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the Dictionary. Return Value: This method will return true if the Dictionary contains an element with the specified key otherwise, it returns false.


1 Answers

Try using the HashSet<T> class.

Edit: I spent a long long time doing exactly what you did until I just stumbled on this class while reading a blog.

like image 198
Jake Avatar answered Oct 04 '22 00:10

Jake