?
Solved

Avoid duplicates in a Dictionary

Posted on 2009-04-23
8
Medium Priority
?
2,086 Views
Last Modified: 2012-06-21
I have a dictionary like this : Dictioanry<Guid,int>

In it , for example, i have 1234 , 5
                                        6666, 0

I'm looping and some GUID's might be duplicated which wont work because i cant have duplicate keys. Anyway to say: if this GUID is already in the dictionary, dont add it.
0
Comment
Question by:Camillia
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:Wikkard
ID: 24221386
You could use a hashtable which wont allow duplicate keys:

System.Collections.Hashtable h = new Hashtable();
            h.Add("sdf", "Sdf");
            h.Add("sdf", "123123");//this line will throw exception because of duplicate key


I may have misunderstood your use of the term GUID, but the whole point of guids is that they will be unique. The chances of you generating a duplicate guid is very small, however still exists.


0
 
LVL 8

Expert Comment

by:Wikkard
ID: 24221416
I just checked and the code below will cause and exception in the same manner as the hashtable example.

  System.Collections.Generic.Dictionary<string, string> d = new System.Collections.Generic.Dictionary<string,string>();
            d.Add("1234", "Sdfsdfsd");
            d.Add("1234", "sdfsdfsdfsdfd");


This should happen regardless of the keys type (guid, string whatever), it just wont allow duplicate keys.

0
 
LVL 9

Expert Comment

by:tetorvik
ID: 24221744
prior to adding new item into dictionary double-check if the key already exists in it
However if you are getting GUIDs from System.Guid you shouldn't get any duplicates.
            Dictionary<string, string> dict = new Dictionary<string, string>();
            string guid = System.Guid.NewGuid().ToString();
            if (!dict.ContainsKey(guid))
                dict.Add(guid, "your value");

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:DreamsTech
ID: 24222086
As far I know ,this validation is already in place ,the Dictonary Component will never allow a duplicate key to be added;and it will throw an exception.

like Dict.Add("key",1)
Dict.Add("key",2)
executing these will throw an exception as "key" is a duplicate key
0
 
LVL 9

Expert Comment

by:tetorvik
ID: 24222238
Yes it's in place, but why would you like to have an exception to be thrown if there is a single line of code available that avoids it?
0
 
LVL 7

Author Comment

by:Camillia
ID: 24223791
DreamsTech: is right. The validation is in place and it throws an error and crashes the app. I want to DO A CHECK : if key exits in the dictionary, skip and dont insert.

 How can I do this???
0
 
LVL 9

Accepted Solution

by:
tetorvik earned 2000 total points
ID: 24223865
Do it like I said
        //if the key don't exits in the dictionary
        if (!dict.ContainsKey(guid))
                dict.Add(guid, "your value"); //insert new item

Or am I missing something here?
0
 
LVL 7

Author Comment

by:Camillia
ID: 24223974
ah yes, let me try. will post back.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question