c# How to iterate through a dictionary and compare values?

I'm teaching myself c# and working on my own mini project. The program populates an array with random numbers, the program returns the number (0-15) and the number of occurrences it appears in the array. I stored these values in a dictionary as I wanted to sort the values without losing the key mapped to it.

The sorted values are then stored into another dictionary and now I want to be able to iterate through the dictionary and get the key with the highest value. In other words print to the console the number with the most occurrences. As the dictionary is sorted, the last number will be the highest value.

However there could be more than one number tied for the most occurrences and that's where I'm stuck on. If the numbers 4,5,6,7 all appear the most number of times, i want to be able to print that to the console.

      Dictionary<int, int> dic = new Dictionary<int, int>();
      //iterates through numbers 0-15
      for (int y = 0; y <= 15; y++)
       {   
            int m = 0;
            //iterates through entire array
            for (int i = 0; i < Arr.Length; i++)
            { 
                //comparisons 
                if (y == Arr[i])
                {
                    m++;

                }

            }
            //Inserts number and count into the dictionary
            dic.Add(y,m);

        }
        //Sorts the dictionary and adds the sorted one into a new dictionary
        Dictionary<int, int> dic2 = new Dictionary<int, int>();
        foreach (KeyValuePair<int, int> value in dic.OrderBy(key => key.Value))
        {
            Console.WriteLine("{0} appears {1} times ", value.Key, value.Value);
            dic2.Add(value.Key, value.Value);
        }

        //Finds the keys with most common occurance
        KeyValuePair<int, int> e = dic2.Last();

        foreach (KeyValuePair<int, int> comp in dic2)
        {

            if (dic.Last() == dic[comp])
                {
                    //something goes here
                    Console.WriteLine("Most common number is {0}", e.Key);
                }
        }

I'm not sure whether to use indexes to compare using the key or if there is another way to do this like I have tried above, using a foreach loop

Jon Skeet
people
quotationmark

I wouldn't use the current approach at all, to be honest - you're doing much more work than you need to. LINQ gives you much better tools than this. You can use GroupBy to make it all cleaner:

var pairs = array.GroupBy(x => x)
                 .Select(g => new { Key = g.Key, Count = g.Count() }
                 .OrderByDescending(pair => pair.Count)
                 .ToList();

That gets you all the key/count pairs, most-frequent first. The display part should then be reasonably simple, e.g.

// Note: this relies on the initial array being non-empty
var highestCount = pairs.First().Count;
foreach (var pair in pairs.TakeWhile(pair => pair.Count == highestCount))
{
    Console.WriteLine("{0}: {1}", pair.Key, pair.Count);
}

Just to be clear, the code above replaces all the code in your question. You don't need a Dictionary<,> at all.

people

See more on this question at Stackoverflow