TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Find Maximum and Minimum Occurring Character in a String

Program to Find Maximum and Minimum Occurring Character in a String on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

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.

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

  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

string = "grass is greener on the other side";
freq = [None] * len(string);
minChar = string[0];
maxChar = string[0];
 
#Count each word in given string and store in array freq
for i in range(0, len(string)):
    freq[i] = 1;
    for j in range(i+1, len(string)):
        if(string[i] == string[j] and string[i] != ' ' and string[i] != '0'):
            freq[i] = freq[i] + 1;
            
            #Set string[j] to 0 to avoid printing visited character
            string = string[ : j] + '0' + string[j+1: ];
            
#Determine minimum and maximum occurring characters
min = max = freq[0];
for i in range(0, len(freq)):
    
    #If min is greater than frequency of a character
    #then, store frequency in min and corresponding character in minChar
    if(min > freq[i] and freq[i] != '0'):
        min = freq[i];
        minChar = string[i];
    #If max is less than frequency of a character 
    #then, store frequency in max and corresponding character in maxChar
    if(max < freq[i]):
        max = freq[i];
        maxChar = string[i];
 
print("Minimum occurring character: " + minChar);
print("Maximum occurring character: " + maxChar);

Output:

Minimum occurring character: a
Maximum occurring character: e

C

#include <stdio.h>
#include <string.h>
 
int main()
{
    char string[] = "grass is greener on the other side";
    int i, j, min, max, length = strlen(string);
    char minChar = string[0], maxChar = string[0];
    int freq[length];
    
    //Count each word in given string and store in array freq
    for(i = 0; i < length; i++) {
        freq[i] = 1;
        for(j = i+1; j < length; j++) {
            if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') {
                freq[i]++;
                
                //Set string[j] to 0 to avoid printing visited character
                string[j] = '0';
            }
        }
    }
    
    //Determine minimum and maximum occurring characters
    min = max = freq[0];
    for(i = 0; i < length; i++) {
        
        //If min is greater than frequency of a character 
        //then, store frequency in min and corresponding character in minChar
        if(min > freq[i] && freq[i] != '0') {
            min = freq[i];
            minChar = string[i];
        }
        //If max is less than frequency of a character 
        //then, store frequency in max and corresponding character in maxChar
        if(max < freq[i]) {
            max = freq[i];
            maxChar = string[i];
        }
    }
    
    printf("Minimum occurring character: %c\n", minChar);
    printf("Maximum occurring character: %c", maxChar);
        
    return 0;
}

Output:

Minimum occurring character: a
Maximum occurring character: e

JAVA

public class Characters 
{
     public static void main(String[] args) {
        String str = "grass is greener on the other side";
        int[] freq = new int[str.length()];
        char minChar = str.charAt(0), maxChar = str.charAt(0);
        int i, j, min, max;        
        
        //Converts given string into character array
        char string[] = str.toCharArray();
        
        //Count each word in given string and store in array freq
        for(i = 0; i < string.length; i++) {
            freq[i] = 1;
            for(j = i+1; j < string.length; j++) {
                if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') {
                    freq[i]++;
                    
                    //Set string[j] to 0 to avoid printing visited character
                    string[j] = '0';
                }
            }
        }
        
        //Determine minimum and maximum occurring characters
        min = max = freq[0];
        for(i = 0; i <freq.length; i++) {
            
            //If min is greater than frequency of a character 
            //then, store frequency in min and corresponding character in minChar
            if(min > freq[i] && freq[i] != '0') {
                min = freq[i];
                minChar = string[i];
            }
            //If max is less than frequency of a character 
            //then, store frequency in max and corresponding character in maxChar
            if(max < freq[i]) {
                max = freq[i];
                maxChar = string[i];
            }
        }
        
        System.out.println("Minimum occurring character: " + minChar);
        System.out.println("Maximum occurring character: " + maxChar);
    }
}

Output:

Minimum occurring character: a
Maximum occurring character: e

C#

using System;
                    
public class Characters
{    
    public static void Main()
    {
        String str = "grass is greener on the other side";
        int[] freq = new int[str.Length];
        char minChar = str[0], maxChar = str[0];
        int i, j, min, max;        
        
        //Converts given string into character array
        char[] string1 = str.ToCharArray();
        
        //Count each word in given string and store in array freq
        for(i = 0; i < string1.Length; i++) {
            freq[i] = 1;
            for(j = i+1; j < string1.Length; j++) {
                if(string1[i] == string1[j] && string1[i] != ' ' && string1[i] != '0') {
                    freq[i]++;
                    
                    //Set string1[j] to 0 to avoid printing visited character
                    string1[j] = '0';
                }
            }
        }
        
        //Determine minimum and maximum occurring characters
        min = max = freq[0];
        for(i = 0; i < freq.Length; i++) {
            
            //If min is greater than frequency of a character 
            //then, store frequency in min and corresponding character in minChar
            if(min > freq[i] && freq[i] != '0') {
                min = freq[i];
                minChar = string1[i];
            }
            //If max is less than frequency of a character 
            //then, store frequency in max and corresponding character in maxChar
            if(max < freq[i]) {
                max = freq[i];
                maxChar = string1[i];
            }
        }
        
        Console.WriteLine("Minimum occurring character: " + minChar);
        Console.WriteLine("Maximum occurring character: " + maxChar);
    }
}

Output:

Minimum occurring character: a
Maximum occurring character: e

PHP

<!DOCTYPE html>
<html>
<body>
<?php
$string = "grass is greener on the other side";
$freq = array();
$minChar = $maxChar = $string[0];        
 
//Count each word in given string and store in array freq
for($i = 0; $i < strlen($string); $i++) {
    array_push($freq, 1);
    for($j = $i+1; $j < strlen($string); $j++) {
        if($string[$i] == $string[$j] && $string[$i] != ' ' && $string[$i] != '0') {
            $freq[$i]++;
            
            //Set $string[$j] to 0 to avoid printing visited character
            $string[$j] = '0';
        }
    }
}
 
//Determine minimum and maximum occurring characters
$min = $max = $freq[0];
for($i = 0; $i < count($freq); $i++) {
    
    //If min is greater than frequency of a character 
    //then, store frequency in min and corresponding character in minChar
if($min > $freq[$i] && $freq[$i] != '0') {
    $min = $freq[$i];
    $minChar = $string[$i];
}
//If max is less than frequency of a character 
//then, store frequency in max and corresponding character in maxChar
if($max < $freq[$i]) {
    $max = $freq[$i];
    $maxChar = $string[$i];
}
}
 
print("Minimum occurring character: " . $minChar);
print("<br>Maximum occurring character: " . $maxChar);
?>
</body>
</html>

Output:

Minimum occurring character: a
Maximum occurring character: e

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf