Find out max duplicate number between 1 to N numbers How can I make this method better?

int[] _input = new int[] { 5,45,184,594,14,8,5,184,1841,184,594,5,78,594,14,8,18,4,18,14,8,594};


Dictionary&lt;int, int&gt; _inputDictionary = new Dictionary&lt;int, int&gt;();

foreach (int i in _input)
{
    if (_inputDictionary.ContainsKey(i))
    {

        _inputDictionary[i] = ++_inputDictionary[i] ;

    }
    else
    {
        _inputDictionary.Add(i,1);
    }
}

Console.WriteLine();
Console.WriteLine("Maximum number repeated is " + _inputDictionary.OrderByDescending(g =&gt; g.Value).First().Key + " and it is repeated " + _inputDictionary.OrderByDescending(g =&gt; g.Value).First().Value+" times");
You can use GroupBy to count the duplicates.

Then you should sort the result and get the first item once, instead of sorting it each time you use it:

int[] _input = new int[] {
  5, 45, 184, 594, 14, 8, 5, 184, 1841, 184, 594, 5, 78, 594, 14, 8, 18, 4, 18, 14, 8, 594
};

IGrouping&lt;int, int&gt; max =
  _input.GroupBy(n =&gt; n)
  .OrderByDescending(g =&gt; g.Count())
  .First();

Console.WriteLine(
  "Maximum number repeated is " + max.Key +
  " and it is repeated " + max.Count() + " times"
);