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

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

  • STEP 1: START
  • STEP 2: DEFINE String str = “grass is greener on the other side”
  • STEP 3: INITIALIZE minChar, maxChar.
  • STEP 4: DEFINE i, j, min, max.
  • STEP 5: CONVERT str into char string[].
  • STEP 6: SET i =0. REPEAT STEP 7 to STEP 11 UNTIL i
  • STEP 7: SET array freq[i] =1
  • STEP 8: SET j =i+1. REPEAT STEP 9 to STEP 10 UNTIL j
  • STEP 9: IF (string[i] == string[j] && string[i] != ‘ ‘ && string[i] != ‘0’)
                  then
                  freq[i] = freq[i] + 1
                  SET string[j] = 0
  • STEP 10: j = j +1
  • STEP 11: i = i + 1
  • STEP 12: SET min = max = freq[0]
  • STEP 13: SET i =0. REPEAT STEP 14 to STEP 16 UNTIL i
  • STEP 14: IF(min>freq[i] && freq[i]!=0) then
                  min = freq[i]
                  minChar[] = string[i]
  • STEP 15: IF max is lesser than freq[i]then
                  max = freq[i]
                  maxChar[] = string[i]
  • STEP 16: i =i +1
  • STEP 17: PRINT minChar
  • STEP 18: PRINT maxChar
  • STEP 19: END

Program:

  1. public class Characters     
  2.    
  3.    public static void main(String[] args) {    
  4.       String str = “grass is greener on the other side”;    
  5.       int[] freq = new int[str.length()];    
  6.       char minChar = str.charAt(0), maxChar = str.charAt(0);    
  7.       int i, j, min, max;            
  8.           
  9.       //Converts given string into character array    
  10.       char string[] = str.toCharArray();    
  11.           
  12.       //Count each word in given string and store in array freq    
  13.       for(i = 0; i < string.length; i++) {    
  14.           freq[i] = 1;    
  15.           for(j = i+1; j < string.length; j++) {    
  16.               if(string[i] == string[j] && string[i] != ‘ ‘ && string[i] != ‘0’) {    
  17.                   freq[i]++;    
  18.                       
  19.                   //Set string[j] to 0 to avoid printing visited character    
  20.                   string[j] = ‘0’;    
  21.               }    
  22.           }    
  23.       }    
  24.           
  25.       //Determine minimum and maximum occurring characters    
  26.       min = max = freq[0];    
  27.       for(i = 0; i <freq.length; i++) {    
  28.               
  29.           //If min is greater than frequency of a character     
  30.           //then, store frequency in min and corresponding character in minChar    
  31.           if(min > freq[i] && freq[i] != ‘0’) {    
  32.               min = freq[i];    
  33.               minChar = string[i];    
  34.           }    
  35.           //If max is less than frequency of a character     
  36.           //then, store frequency in max and corresponding character in maxChar    
  37.           if(max < freq[i]) {    
  38.               max = freq[i];    
  39.               maxChar = string[i];    
  40.           }    
  41.       }    
  42.           
  43.       System.out.println(“Minimum occurring character: ” + minChar);    
  44.       System.out.println(“Maximum occurring character: ” + maxChar);    
  45.   }    
  46.    

Output:

Minimum occurring character: a
Maximum occurring character: e

Leave a Comment