Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
How do you extract the key value from the dictionary?
C#
private Dictionary<string, double> g_masses = new Dictionary<string, double>();

          private void PopulateWeightDictionary()
          {
               g_masses.Add("K", 3);
               g_masses.Add("R", 1);
               g_masses.Add("S", 5);
               g_masses.Add("L", 22);
               g_masses.Add("G", 1);

}
Posted
Updated 12-Dec-11 7:18am
v3

Personally, I would sort the string and just count the repetitions:
C#
string s = "KRRSLSG";
char[] data = s.ToCharArray();
Array.Sort(data);
char last = '\0';
Dictionary<char, int> counts = new Dictionary<char, int>();
foreach (char c in data)
    {
    if (last == c)
        {
        counts[c]++;
        }
    else
        {
        last = c;
        counts.Add(c, 1);
        }
    }
 
Share this answer
 
Comments
Abhinav S 12-Dec-11 12:38pm    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900