-1

I am using ASP.NET MVC and I was populating a Dictionary and was getting the An item with the same key has already been added. error now what I am trying to is if the key already exists in the Dictionary, make it unique by adding a number next to the key name.

if (!RedBlueData.ContainsKey(model))
{
    RedBlueData.Add(model, redBlue);
}
else
{
    RedBlueData.Add(model, redBlue);
}

The expected result would be (lets say the key string name is "Item" then if there is going to be another key with the name "Item" I would like to name it "Item 2" and if there is a 3rd or 4th have the name as "Item 3" and "Item 4"

I hope this makes sense.

theKunz
  • 444
  • 4
  • 12
user979331
  • 11,039
  • 73
  • 223
  • 418
  • 3
    If you add a new item with a new key, how do you go back and find that item with the new key? If you don't need to go back and look at it, whats the point of using a dictionary? – Erik Philips Mar 19 '18 at 20:38
  • 3
    Seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – maccettura Mar 19 '18 at 20:55
  • 1
    What's the point of a conditional where both branches do the same thing? – spender Mar 19 '18 at 21:00
  • @spender I think OP wants us to fill in the rest to make the key different on the false condition – maccettura Mar 19 '18 at 21:04
  • @user979331 Have you considered an approach like https://stackoverflow.com/questions/2101069/c-sharp-dictionary-one-key-many-values ? – mjwills Mar 19 '18 at 21:22
  • @mjwills So many .Net classes not being first class citizens like `Lookup` makes me sad. – NetMage Mar 19 '18 at 21:59
  • Regardless of whether it makes sense or not, if dictionary already contains a key, then you need to make some decisions. In the simple case, check for `"key " + SomeNumber.ToString()` and keep incrementing the number until you don't find the key in the dictionary. If a key that already contains a number, e.g. `"The answer is 42"`, is handed to your code things get more interesting. – HABO Mar 19 '18 at 22:06
  • Do you really want unique keys that have no context? If not, look into other data structures. @NetMage has a good one, or you could just use KeyValuePair. It is basically a non-unique dictionary, and can be enumerated so you can get all values for a given key, or visa versa. Anytime you find yourself asking how you can get around the rules defining an object, stop and ask if that object is the correct tool for the job. – user7396598 Mar 19 '18 at 22:44

1 Answers1

0

How about building your own multi-dictionary by using a Dictionary<TKey,List<TValue>> instead of Dictionary<TKey,TValue>?

if (RedBlueData.TryGetValue(model, out var colorList))
    colorList.Add(redBlue);
else
    RedBlueData.Add(model, new List<Color> { redBlue });

I used type Color for the type of redBlue since I didn't know what it was.

NetMage
  • 26,163
  • 3
  • 34
  • 55
  • 1
    And here you have a basic hash table. :) – itsme86 Mar 19 '18 at 22:43
  • @itsme86 How so...? `Dictionary` is a hash table implementation. – NetMage Mar 19 '18 at 22:49
  • It is with the multiple-value value type such as a `List<>` like you have. There needs to be some way to deal with collisions. – itsme86 Mar 19 '18 at 22:55
  • @itsme86 I don't think you understand `Dictionary`. Collisions at the key level inside the hash table of the implementation of `Dictionary` are handled by the class already. How duplicate (key) entries being added to the data structure are handled has nothing to do with a hash table, but instead depend on the specific type of data structure e.g. associative array versus multimap. Hash table is an implemenation detail, and collisions are too. – NetMage Mar 19 '18 at 22:59
  • 1
    I feel like you're being purposely obtuse. Here, you can read [this](https://en.wikipedia.org/wiki/Hash_table) if you still have questions on what a hash table is. – itsme86 Mar 19 '18 at 23:04
  • @itsme86 No, I just think terminology in programming should be precise. I had read the article - have you? "However, if the key of the new item exactly matches the key of an old item, the associative array typically erases the old item and overwrites it with the new item, so every item in the table has a unique key." – NetMage Mar 19 '18 at 23:06
  • You're still trying? You're cherry picking. Try this one: "Ideally, the hash function will assign each key to a unique bucket, but most hash table designs employ an imperfect hash function, which might cause hash collisions where the hash function generates the same index for more than one key. Such collisions must be accommodated in some way." e.g. a list. In fact there's a whole section on collision resolution there you should read. – itsme86 Mar 20 '18 at 14:31
  • @itsme86 Already covered: "Collisions at the key level inside the hash table of the implementation of `Dictionary` are handled by the class already." Implentation of hash table inside `Dictionary` and duplicate keys by user of `Dictionary` are not related. – NetMage Mar 20 '18 at 18:16