Write a Java Program to Find Minimum Occurring Character in a String with an example. First, we declared the charFreq integer array of given string length. Next, we converted the minOccStr string to minOccArr character array using toCharArray. Next, assigned the first character as the minchar.
The first for loop is used to assign the character frequency to charFreq array. The other for loop is to find the minimum occurrence of a character.
import java.util.Scanner; public class MinOccurChar1 { private static Scanner sc; public static void main(String[] args) { String minOccStr; int i, min; sc= new Scanner(System.in); System.out.print("\nEnter String to find Minimum Char Occurrence = "); minOccStr = sc.nextLine(); int[] charFreq = new int[minOccStr.length()]; char[] minOccArr = minOccStr.toCharArray(); char minChar = minOccStr.charAt(0); for(i = 0; i < minOccArr.length; i++) { charFreq[i] = 1; for(int j = i + 1; j < minOccArr.length; j++) { if(minOccArr[i] == minOccArr[j] && minOccArr[i] != ' ' && minOccArr[i] != '0') { charFreq[i]++; minOccArr[j] = '0'; } } } min = charFreq[0]; for(i = 0; i < charFreq.length; i++) { if(min > charFreq[i] && charFreq[i] != '0') { min = charFreq[i]; minChar = minOccArr[i]; } } System.out.println("\nThe Minimum Occurring Character = " + minChar); System.out.format("'%c' Character Occurs %d Times ", minChar, min); } }
Java Program to Find Minimum Occurring Character in a String Example 2
import java.util.Scanner; public class MinOccurChar2 { private static Scanner sc; public static void main(String[] args) { String minOccStr; int i, min = 0; int[] charFreq = new int[256]; sc= new Scanner(System.in); System.out.print("\nEnter String to find Minimum Char Occurrence = "); minOccStr = sc.nextLine(); for(i = 0; i < minOccStr.length(); i++) { charFreq[minOccStr.charAt(i)]++; } for(i = 0; i < 256; i++) { if(charFreq[i] != 0) { if(charFreq[min] == 0 || charFreq[i] < charFreq[min]) { min = i; } } } System.out.format("\nThe Minimum Occurring Character = %c", min); System.out.format("\n'%c' Character Occurs %d Times ", min, charFreq[min]); } }
Enter String to find Minimum Char Occurrence = java
The Minimum Occurring Character = j
'j' Character Occurs 1 Times
This Java program to return minimum occurred string character is the same as the above. Here, we separated the logic using Java functions.
import java.util.Scanner; public class MinOccurChar3 { private static Scanner sc; public static void main(String[] args) { String minOccStr; sc= new Scanner(System.in); System.out.print("\nEnter String to find Minimum Char Occurrence = "); minOccStr = sc.nextLine(); minimumCharOccurence(minOccStr); } public static void minimumCharOccurence(String minOccStr) { int i, min = 0; int[] charFreq = new int[256]; for(i = 0; i < minOccStr.length(); i++) { charFreq[minOccStr.charAt(i)]++; } for(i = 0; i < 256; i++) { if(charFreq[i] != 0) { if(charFreq[min] == 0 || charFreq[i] < charFreq[min]) { min = i; } } } System.out.format("\nThe Minimum Occurring Character = %c", min); System.out.format("\n'%c' Character Occurs %d Times ", min, charFreq[min]); } }
Enter String to find Minimum Char Occurrence = tutorialgateway
The Minimum Occurring Character = e
'e' Character Occurs 1 Times
Please refer to the following string programs.
- Count the Total Occurrence of a Character in a String
- Find All Occurrences of a Character in a String
- First Character Occurrence in a String
- Replace First Character Occurrence in a String
- Remove First Character Occurrence in a String
- Last Character Occurrence in a String
- Replace Last Character Occurrence in a String
- Remove the Last Character Occurrence in a String
- First and Last Character in a String
- Remove The First and Last Character in a String
- Maximum Occurring Character in a String
- Frequency of each Character in a String
- Remove All Occurrences of a Character in a String