How to find the longest repeating character in a string with C#

1 Answer

0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        string str = "aabbbbhhhhhhhdddefgggg88";
        
        string longest_repeating_character = new string(str.Select((ch, index) => str.Substring(index).TakeWhile(e => e == ch))
                                                    .OrderByDescending(e => e.Count())
                                                    .First().ToArray());

        Console.WriteLine(longest_repeating_character);
    }
}



/*
run:

hhhhhhh

*/

 



answered Sep 1, 2023 by avibootz
...