Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary with duplicate Key [duplicate]

Possible Duplicate:
Is there an alternative to Dictionary/SortedList that allows duplicates?

I am looking for a Dictionary sort of class which can have duplicate Keys.

I search about it, and found LookUp class can use to store duplicate keys, but It has no default constructor, So we can't initialize it without any other object to LookUp.

But I have no any such object initially from which I can initialize a LookUp object.

So, My question is, Is there any class in .Net framework 3.5, which behave like Dictionary but allow me to have duplicate keys like LookUp?

like image 359
Yograj Gupta Avatar asked Nov 01 '12 20:11

Yograj Gupta


People also ask

Can dictionary contain duplicate keys?

[C#] Dictionary with duplicate keys The Key value of a Dictionary is unique and doesn't let you add a duplicate key entry. To accomplish the need of duplicates keys, i used a List of type KeyValuePair<> .

Can KEY be repeated in dictionary python?

Python dictionary doesn't allow key to be repeated.


3 Answers

You could create a list of key value pairs.

List<KeyValuePair<string,int>>
like image 56
madeFromCode Avatar answered Oct 16 '22 18:10

madeFromCode


A Dictionary, by definition, will never be able to have multiple keys with the same value. (If you looked up a key whatever would you return?) Even a Lookup, which you refer to, doesn't allow it. What you can do is have each key refer to multiple values (logically, not technically). This is done by having a dictionary in which the value is a data structure of some sort (for example, a List) that contains all of the values that correspond to that particular key.

like image 22
Servy Avatar answered Oct 16 '22 20:10

Servy


You can compose a type yourself by using a dictionary of lists, Dictionary<TKey, List<TValue>>

You can create a class inheriting from that class and add suitable add-methods etc that handles creating a new list for the first item on a given key.

like image 3
Albin Sunnanbo Avatar answered Oct 16 '22 20:10

Albin Sunnanbo