Find character with most occurrences in string?

前端 未结 10 1044
一生所求
一生所求 2020-11-27 17:49

For example, I have a string:

\"abbbbccd\"

b has the most occurrences. When using C++, the easiest way to handle this is inser

10条回答
  •  春和景丽
    2020-11-27 18:34

    string testString = "abbbbccd";
    var charGroups = (from c in testString
                        group c by c into g
                        select new
                        {
                            c = g.Key,
                            count = g.Count(),
                        }).OrderByDescending(c => c.count);
    foreach (var group in charGroups)
    {
        Console.WriteLine(group.c + ": " + group.count);
    }
    

提交回复
热议问题