C# program to find the maximum occurring character in a String

C# program to find the maximum occurring character in a string:

In this post, we will learn how to find the maximum occurring character in a string. The program will take the string as input from the user. It will find the maximum occurring character in that string and print the character that appeared max number of times in the string.

If we have more than one character with an equal number of occurrences, it will print the character that will come first in alphabetical order.

For example, if the string is hello world, it will print l, the count of which is 3 in the string.

Algorithm to find the maximum occurring character in a string:

To find the maximum occurring character in a string in C#, we will use the below algorithm:

  • Take the string as input from the user.
  • Initialize one array of size 256. Initialize each item in the array as 0.  We are initializing this array to hold the total count of each character in that string. Each index in the array defines the ASCII value of a character.
  • Iterate over the characters of the string one by one. For each character’s ASCII value, increment the value in the array by 1. For example, if the character is a, increment the value in the array at index 97, because 97 is the ASCII value of a.
  • Once the loop will end, we will have to iterate through the array to find the index with the maximum value. This index is the ASCII of the character that occurs the highest number of times in that string. Print the character for that ASCII value.

C# program:

Below is the complete C# program:

using System;
namespace Main
{
    public class Example
    {

        private static char getMaxOccurrenceCharacter(String str)
        {
            int[] countArray = new int[256];
            int maxValue = 0;
            char resultChar = '\0';

            for (int i = 0; i < str.Length; i++)
            {
                countArray[str[i]]++;

                if (countArray[str[i]] > maxValue)
                {
                    maxValue = countArray[str[i]];
                    resultChar = str[i];
                }
            }

            return resultChar;
        }

        public static void Main()
        {
            Console.WriteLine("Enter a string :");
            String givenStr = Console.ReadLine();

            Console.WriteLine("The character with maximum occurrence is : " + getMaxOccurrenceCharacter(givenStr));

        }
    }
}

Download it on GitHub

  • The getMaxOccurrenceCharacter method is used to get the character with the highest occurrences in a string. It takes one string as its parameter and returns the character.
  • The countArray is an array to keep the count of each character.
  • The integer variable maxValue is used to hold the maximum number of occurrences for a character found in the array and the character variable resultChar is used to assign the final result character.
  • We are using only one for loop in this program. This loop runs through the characters of the string and updates the countArray. It also updates the maxValue and resultChar if any character with occurrences larger than the current maximum occurrence is found.
  • This method returns the value of resultChar.

C# find the maximum occurring character in a string

If you run this program, it will print output as below:

Enter a string :
hello worldddd

The character with maximum occurrence is : d

You might also like: