score:1

Accepted answer

This should work fine

var topThreeMostOccuring = arr.GroupBy(x => x)
.OrderByDescending(g => g.Count())
.SelectMany(x => x.Take(1)).Take(3);

score:1

You can use Linq :

var top3 = list.GroupBy(x => x)
               .OrderByDescending(x => x.Count())
               .Take(3)
               .Select(x => x.Key);

Console.WriteLine(string.Join(" ", top3)); // output 5 3 4

score:0

Hope this helps you

 var list = new List<int>();

        list.Add(5);
        list.Add(5);
        list.Add(27);
        list.Add(4);
        list.Add(3);
        list.Add(4);
        list.Add(4);
        list.Add(29);
        list.Add(3);
        list.Add(5);
        list.Add(4);



       var TheMostoccurring =  list.GroupBy(n => n).OrderByDescending(m => m.Count()).Select(m => m.Key).ToList().Take(3);



        foreach (var item in TheMostoccurring)
        {
            Console.WriteLine(item);
        }

Related Articles