How to find the maximum repeating item in a list with C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main() {
        List<string> lst = new List<string>();
        
        lst.Add("c#");
        lst.Add("c");
        lst.Add("c++");
        lst.Add("c");
        lst.Add("c#");
        lst.Add("java");
        lst.Add("c#");
        
        string maxRepeated = lst.GroupBy(s => s)
                            .OrderByDescending(s => s.Count())
                            .First().Key;

        Console.Write(maxRepeated);
    }
}



/*
run:

c#

*/

 



answered Aug 29, 2022 by avibootz

Related questions

1 answer 198 views
2 answers 238 views
1 answer 206 views
2 answers 365 views
1 answer 207 views
...