Signup/Sign In
PUBLISHED ON: APRIL 1, 2021

Java Program To Find Maximum And Minimum Occurring Character

In this tutorial, we will learn how to Find the minimum and maximum occurring characters. Here, we will first iterate through the string and then we will calculate the frequency of each element. Lastly, we will print the characters with the highest and the least frequency. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the string: Apple

Output: Maximum Occurrence : p

Minimum Occurrence: A

Program 1: Find the Minimum and Maximum Occurring Character

In this program, we will see how to calculate the minimum and maximum occurring characters in a string when the string is user-defined. Here, we will ask the user to initialize the array and then we will find out the minimum and maximum occurring character

Algorithm:

  1. Start

  2. Declare a string

  3. Ask the user to initialize it.

  4. Declare an array to store the frequency of the elements.

  5. Declare variables for minimum and maximum occurring characters and assign them to 0.

  6. Convert the string to a character array.

  7. Use two for loops for the same.

  8. Use the first for loop to iterate through each character in the string.

  9. Select the characters and initialize their corresponding frequency in the frequency array to 1.

  10. Use the second for loop to compare the selected character with the rest of the characters present in the string.

  11. Again use two for loops to iterate over the frequency array.

  12. Use the min and max variable to store the count of minimum and maximum occurring characters in the string.

  13. Iterate over the loop and compare the count of each character stored in freq with min and max.

  14. If the count stored in freq is less than the value of min, then store that count in the min and corresponding character in minChar.

  15. If the count stored in freq is more than the value of max, then store that count in max and corresponding character in maxChar.

  16. Now, the minChar will store the minimum occurring character and maxChar will store the maximum occurring character.

  17. Print the minimum and maximum occurring characters.

  18. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to find the maximum and minimum occurring character in a string
import java.util.*;
public class Main  
{
   public static void main(String[] args) 
   {    
       //Take input from the user 
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the string: ");
        String str=sc.nextLine();
      int[] freq = new int[str.length()];    
      char minChar = str.charAt(0), maxChar = str.charAt(0);    
      int i, j, min, max;            
      char string[] = str.toCharArray();    
      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]++;    
                      
                  string[j] = '0';    
              }    
          }    
      }    
      min = max = freq[0];    
      for(i = 0; i <freq.length; i++) 
	  {    
          if(min > freq[i] && freq[i] != '0') 
		  {    
              min = freq[i];    
              minChar = string[i];    
          }    
          if(max < freq[i]) 
		  {    
              max = freq[i];    
              maxChar = string[i];    
          }    
      }             
      System.out.println("Minimum occurring character: " + minChar);    
      System.out.println("Maximum occurring character: " + maxChar);    
  }  
}


Enter the string: Find the minimum and maximum occurring character
Minimum occurring character: F
Maximum occurring character: m

Program 2: Find the Minimum and Maximum Occurring Character

In this program, we will see how to calculate the minimum and maximum occurring characters in a string when the string is pre-defined in the program.

Algorithm:

  1. Start

  2. Declare a string

  3. Initialize it.

  4. Declare an array to store the frequency of the elements.

  5. Declare variables for minimum and maximum occurring characters and assign them to 0.

  6. Convert the string to a character array.

  7. Use two for loops for the same.

  8. Use the first for loop to iterate through each character in the string.

  9. Select the characters and initialize their corresponding frequency in the frequency array to 1.

  10. Use the second for loop to compare the selected character with the rest of the characters present in the string.

  11. Again use two for loops to iterate over the frequency array.

  12. Use the min and max variable to store the count of minimum and maximum occurring characters in the string.

  13. Iterate over the loop and compare the count of each character stored in freq with min and max.

  14. If the count stored in freq is less than the value of min, then store that count in the min and corresponding character in minChar.

  15. If the count stored in freq is more than the value of max, then store that count in max and corresponding character in maxChar.

  16. Now, the minChar will store the minimum occurring character and maxChar will store the maximum occurring character.

  17. Print the minimum and maximum occurring characters.

  18. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to find the maximum and minimum occurring character in a string

public class Main  
{
   public static void main(String[] args) 
   {  
        String str = "Example of minimum and maximum Character";  
        System.out.println("The entered string is: "+str);
        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 > freq[i] && freq[i] != '0') 
            {  
                min = freq[i];  
                minChar = string[i];  
            }  
            
            if(max < freq[i]) 
            {  
                max = freq[i];  
                maxChar = string[i];  
            }  
        }  
        //Print the minimum and maximum occurring characters  
        System.out.println("Minimum occurring character: " + minChar);  
        System.out.println("Maximum occurring character: " + maxChar);  
    }  
}


The entered string is: Example of minimum and maximum Character
Minimum occurring character: E
Maximum occurring character: m



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.