Java program to check frequency of characters in a string

Check Frequency of Characters in a string

Finding the frequency of a character in a string, means we have to check how many times a particular character is present in that string. Here, we have coded a Java Program for the same. Let’s take an example of a string “prepinsta” . In which we have to check that how many times each of the characters gets repeated in the string.

 

Java program to check frequency of characters in a string

Algorithm

  •  Take string input from user and store it in the variable called “str”.
  • After that make freq array having size of length of string.
  • Convert string to char array .
  • Run for loop start from i=0 to str<length() store 1 to freq array.
  • Run j loop start from i=i+1 to j<str.length().
  • check condition if(string[i] == string[j]) then do freq[i]++  and set string[j]=’0′ .
  • After that display frequency of character one by one.

Code in Java (Check Frequency of Characters)

import java.util.Scanner;

public class FrequencyOfCharactersInAString {
   public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
     System.out.print("Enter String : ");
     String str = sc.nextLine(); 
     int[] freq = new int[str.length()]; 
     int i, j; 

     //Converts given string into character array 
     char string[] = str.toCharArray(); 
     for(i = 0; i <str.length(); i++) { 
        freq[i] = 1; 
          for(j = i+1; j <str.length(); j++) { 
            if(string[i] == string[j]) { 
            freq[i]++; 

           //Set string[j] to 0 to avoid printing visited character 
            string[j] = '0'; 
          } 
       } 
    } 
    //Displays the each character and their corresponding frequency 
    System.out.println("Characters and their corresponding frequencies"); 
    for(i = 0; i <freq.length; i++) { 
       if(string[i] != ' ' && string[i] != '0') 
          System.out.println(string[i] + "-" + freq[i]); 
       } 
   }
}

Output

Enter String : prepinsta
Characters and their corresponding frequencies
p-2
r-1
e-1
i-1
n-1
s-1
t-1
a-1

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

20 comments on “Java program to check frequency of characters in a string”


  • srimadhava1804

    import java.util.*;

    public class main {
    public static void main(String[] args) {
    String str = “prepinsta”;
    Set set = new HashSet();
    for(char c : str.toCharArray()){
    set.add(c);
    }
    List list = new ArrayList(set);
    for(int i=0;i<list.size();i++){
    int count = 0;
    for(int j=0;j<str.length();j++){
    if(list.get(i) == str.charAt(j)){
    count++;
    }
    }
    System.out.println(list.get(i)+" = "+count);
    }
    }
    }


  • rushalisarak

    class Demo {
    public static void main(String[] args) {
    String str = “helloo worldhhhh”;
    str=str.replaceAll(” “,””);
    boolean[] visited = new boolean[str.length()];

    for (int i = 0; i < str.length(); i++) {
    if (!visited[i]) {
    int c = 1;
    char ch = str.charAt(i);
    for (int j = i + 1; j < str.length(); j++) {
    if (ch == str.charAt(j)) {
    c++;
    visited[j] = true;
    }
    }
    visited[i] = true; // Mark current character as visited
    System.out.print(ch + ":" + c + " ");
    }
    }
    }
    }


  • Rajalaxmi

    public class HelloWorld {
    public static void main(String[] args) {
    System.out.println();

    String str=”prepinsta”;

    char c[]=str.toCharArray();
    char temp;
    for(int i=0;i<c.length;i++)
    {
    for(int j=i+1;jc[j])
    {
    temp=c[i];
    c[i]=c[j];
    c[j]=temp;
    }
    }
    }
    for(int i=0;i<c.length;i++)
    {
    int count=1;
    while(i<c.length-1 && c[i]==c[i+1])
    {
    count++;
    i++;
    }
    System.out.println(c[i]+" ; "+count+" ");

    }

    }
    }


  • Abhishek

    public static void main (String[]args)
    {
    HashMap map = new HashMap();
    String str = “prepinsta”;
    char ch[] = str.toCharArray();
    for(Character ch1 : ch){
    map.put(ch1, map.getOrDefault(ch1, 0)+1);
    }
    System.out.println(map);
    }
    }