Find character with most occurrences in string?

前端 未结 10 997
一生所求
一生所求 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:52

    Code:

    class CharCount
    {
        public void CountCharacter()
        {
            int n;
            Console.WriteLine("enter the no. of elements: ");
            n = Convert.ToInt32(Console.ReadLine());
    
            char[] chararr = new char[n];
            Console.WriteLine("enter the elements in array: ");
            for (int i = 0; i < n; i++)
            {
                chararr[i] = Convert.ToChar(Console.ReadLine());
            }
            Dictionary count = chararr.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
    
            foreach(KeyValuePair key in count)
            {
                Console.WriteLine("Occurrence of {0}: {1}",key.Key,key.Value);
            }
    
            Console.ReadLine();
        }
    }
    

提交回复
热议问题