Javatpoint Logo
Javatpoint Logo

Program to find maximum and minimum occurring character in a string

Explanation

In this program, we need to count each character present in the string and find out the maximum and minimum occurring character.

In above example, character 'a' is occurred only once in the string. So, it is minimum occurring character and is highlighted by red. Character e has occurred maximum number of times in the entire string i.e. 6 times. Hence, it is the maximum occurring character and is highlighted by green.

Algorithm

  1. Define a string.
  2. Declare an array freq with the same size as that of string. Array freq will be used to store counts of unique character based upon their index.
  3. Variable minChar represent the minimum occurring character and maxChar represent the maximum occurring character. Initially, they are initialized to first character present in the string.
  4. Two loops will be used. Outer will select a character and initialize corresponding index in freq with 1.
  5. Inner loop will compare the selected character with rest of characters present in the string. If a match found, then increment the freq[i] by 1.
  6. Variable min and max is used to store the count of minimum and maximum occurring character which will initially point to count of first element of freq i.e. freq[0].
  7. Loop through the array freq and compare count of each character stored in freq with min and max.
  8. If count stored in freq is less than value of min, then store that count in min and corresponding character in minChar.
  9. If count stored in freq is more than value of max, then store that count in max and corresponding character in maxChar.
  10. At the end of the loop, minChar will store the minimum occurring character and maxChar will store the maximum occurring character.

Solution

Python

Output:

Minimum occurring character: a
Maximum occurring character: e

C

Output:

Minimum occurring character: a
Maximum occurring character: e

JAVA

Output:

Minimum occurring character: a
Maximum occurring character: e

C#

Output:

Minimum occurring character: a
Maximum occurring character: e

PHP

Output:

Minimum occurring character: a
Maximum occurring character: e

Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA