Explanation
In this program, we need to count each character present in the string and find out the maximum and minimum occurring character.
Grass is greener on the other side
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
- Define a string.
- 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.
- 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.
- Two loops will be used. Outer will select a character and initialize corresponding index in freq with 1.
- 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.
- 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].
- Loop through the array freq and compare count of each character stored in freq with min and max.
- If count stored in freq is less than value of min, then store that count in min and corresponding character in minChar.
- If count stored in freq is more than value of max, then store that count in max and corresponding character in maxChar.
- At the end of the loop, minChar will store the minimum occurring character and maxChar will store the maximum occurring character.
Input:
string = "grass is greener on the other side"
Output:
Minimum occurring character: a
Maximum occurring character: e
Python
Output:
C
Output:
JAVA
Output:
C#
Output:
PHP
Output: