frequency of string in java code example

Example 1: java count frequency of characters in a string

public class Frequency   
{  
     public static void main(String[] args) {  
        String str = "picture perfect";  
        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]);  
        }  
    }  
}

Example 2: find the frequency of characters in a string in java

+++METHOD WITH NONDUP+++
public static String FrequencyOfChars(String str) {

String nonDup = "";

for(int i=0; i < str.length(); i++)

if(!nonDup.contains(""+str.charAt(i)))

nonDup+= ""+str.charAt(i);

 String expectedResult = "";

for( int j=0;j < nonDup.length(); j++) {

int count = 0;

for(int i=0; i < str.length(); i++) {

if(str.charAt(i) == nonDup.charAt(j))

count++;
}

expectedResult +=nonDup.charAt(j)+"" + count;
}

return expectedResult;
}



+++METHOD WITH REPLACE METHOD+++
  
public  static  String  FrequencyOfChars(String str) {

String expectedResult = "";

for( int j=0; j < str.length(); j++) {

int count = 0;

for(int i=0; i < str.length(); i++) {

if(str.charAt(i) == str.charAt(j)) {

count++;
}
}

expectedResult +=str.charAt(j)+"" + count;

str = str.replace(""+str.charAt(j) ,  "" ); 
}

return expectedResult;

}

+++WITH LINKED HASH SET+++
  public  static  String  FrequencyOfChars(String str) {

String b=new LinkedHashSet<>(Arrays.asList(str.split(""))).toString();

  b = b.replace(", ","").replace("[","").replace("]","");

String result="";

for(int j=0; j<b.length();j++) {

int count=0;

for(int i=0; i<str.length(); i++){

if(str.substring(i, i+1).equals(""+str.charAt(j)))

count++;

}

result+=b.substring(j, j+1)+count;

}

return result;

}

++++LAST METHOD USING COLLECTIONS+++
  public static String frequency(String str) {

String nonDup="", result="";

for(int i=0; i < str.length(); i++)

if(! nonDup.contains(""+str.charAt(i)))

nonDup += ""+str.charAt(i);

 

for(int i=0; i < nonDup.length(); i++) {

int num = Collections.frequency( Arrays.asList(str.split("") ) ,    ""+nonDup.charAt( i ) );

result += ""+nonDup.charAt(i) + num;

}

 

return result;

}

Example 3: get frequency of letters java

HashMap<Character, Integer> map = new HashMap<Character, Integer>();
String s = "aasjjikkk";
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    Integer val = map.get(c);
    if (val != null) {
        map.put(c, new Integer(val + 1));
    }
    else {
       map.put(c, 1);
   }
}

Example 4: how to find frequency of each word in string in java

HOW TO COUNT OCCURRENCE OF THE WORDS IN STRING 
(It can also be solved by using nested for loops...)

String str = "I am happy and why not and why are you not happy you should be";
   String [] arr = str.split(" ");
   Map<String, Integer> map = new HashMap<>();

   for (int i=0 ; i < arr.length ; i++){
       if (!map.containsKey(arr[i])){
            map.put(arr[i],1);
           } else{
         map.put(arr[i],map.get(arr[i])+1);
        }
        }
     for(Map.Entry<String, Integer> each : map.entrySet()){

    System.out.println(each.getKey()+" occures " + each.getValue() + " times");
    }

Tags:

Java Example