Javatpoint Logo
Javatpoint Logo

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:

Output:

Minimum occurring character: a
Maximum occurring character: e
Next TopicJava Programs




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA