find the frequency of characters in a string in java

Solutions on MaxInterview for find the frequency of characters in a string in java by the best coders in the world

showing results for - "find the frequency of characters in a string in java"
Mario
20 Mar 2018
1+++METHOD WITH NONDUP+++
2public static String FrequencyOfChars(String str) {
3
4String nonDup = "";
5
6for(int i=0; i < str.length(); i++)
7
8if(!nonDup.contains(""+str.charAt(i)))
9
10nonDup+= ""+str.charAt(i);
11
12 String expectedResult = "";
13
14for( int j=0;j < nonDup.length(); j++) {
15
16int count = 0;
17
18for(int i=0; i < str.length(); i++) {
19
20if(str.charAt(i) == nonDup.charAt(j))
21
22count++;
23}
24
25expectedResult +=nonDup.charAt(j)+"" + count;
26}
27
28return expectedResult;
29}
30
31
32
33+++METHOD WITH REPLACE METHOD+++
34  
35public  static  String  FrequencyOfChars(String str) {
36
37String expectedResult = "";
38
39for( int j=0; j < str.length(); j++) {
40
41int count = 0;
42
43for(int i=0; i < str.length(); i++) {
44
45if(str.charAt(i) == str.charAt(j)) {
46
47count++;
48}
49}
50
51expectedResult +=str.charAt(j)+"" + count;
52
53str = str.replace(""+str.charAt(j) ,  "" ); 
54}
55
56return expectedResult;
57
58}
59
60+++WITH LINKED HASH SET+++
61  public  static  String  FrequencyOfChars(String str) {
62
63String b=new LinkedHashSet<>(Arrays.asList(str.split(""))).toString();
64
65  b = b.replace(", ","").replace("[","").replace("]","");
66
67String result="";
68
69for(int j=0; j<b.length();j++) {
70
71int count=0;
72
73for(int i=0; i<str.length(); i++){
74
75if(str.substring(i, i+1).equals(""+str.charAt(j)))
76
77count++;
78
79}
80
81result+=b.substring(j, j+1)+count;
82
83}
84
85return result;
86
87}
88
89++++LAST METHOD USING COLLECTIONS+++
90  public static String frequency(String str) {
91
92String nonDup="", result="";
93
94for(int i=0; i < str.length(); i++)
95
96if(! nonDup.contains(""+str.charAt(i)))
97
98nonDup += ""+str.charAt(i);
99
100 
101
102for(int i=0; i < nonDup.length(); i++) {
103
104int num = Collections.frequency( Arrays.asList(str.split("") ) ,    ""+nonDup.charAt( i ) );
105
106result += ""+nonDup.charAt(i) + num;
107
108}
109
110 
111
112return result;
113
114}
Alisa
14 Jan 2020
1public class Frequency   
2{  
3     public static void main(String[] args) {  
4        String str = "picture perfect";  
5        int[] freq = new int[str.length()];  
6        int i, j;  
7          
8        //Converts given string into character array  
9        char string[] = str.toCharArray();  
10          
11        for(i = 0; i <str.length(); i++) {  
12            freq[i] = 1;  
13            for(j = i+1; j <str.length(); j++) {  
14                if(string[i] == string[j]) {  
15                    freq[i]++;             
16                    //Set string[j] to 0 to avoid printing visited character  
17                    string[j] = '0';  
18                }  
19            }  
20        }  
21        //Displays the each character and their corresponding frequency  
22        System.out.println("Characters and their corresponding frequencies");  
23        for(i = 0; i <freq.length; i++) {  
24            if(string[i] != ' ' && string[i] != '0')  
25                System.out.println(string[i] + "-" + freq[i]);  
26        }  
27    }  
28}  
Edoardo
12 Jul 2016
1HashMap<Character, Integer> map = new HashMap<Character, Integer>();
2String s = "aasjjikkk";
3for (int i = 0; i < s.length(); i++) {
4    char c = s.charAt(i);
5    Integer val = map.get(c);
6    if (val != null) {
7        map.put(c, new Integer(val + 1));
8    }
9    else {
10       map.put(c, 1);
11   }
12}
queries leading to this page
javahow to find frequency of a character in a stringcount the frequency of character in a given string javafind the frequency of characters in a string using streamsstring frequency count in javacount frequency of elelmet in char array javahow to find frequency of a character in a stringfind the frequency of characters in a string using java streamsfind frequency of each letter javafrequency of string in javajava program to find the frequency of a given character in a stringcalculate frequency of characters in a string javacount the frequency of each character in a array in c 23java program to count the total number of characters in a string while loopfrequency of characters in a string in java hackerrankletter frequency counter javajava program to count the frequency of each character in a stringhow to find frequency of given character in a string in javajava count the number of characters in a stringfrequency of letters in english javahow to find freq of each char in stringreturn a frequency of array of characters javacounting frequency of characters in javafrequecy funtion og strijng javahow to print frequency of each character in a string in java without using string functionsjava program to find frequency of words in a stringfrequency of strings in javafrequency of characters in a string in java using mapcount the number of words in a string javahow to find freequncy of charecters in a sting in javafind a frequency of character in phpprint string with corresponding frequency check frequency of character in string javawrite a java program to count the frequency of words 2c characters in the given line of text java count occurences of specific characters in stringjava application to determine the frequency of each letter in a word frequency of char in string javawrite a program to find and print the frequency of a letter in a string in javacharacter frequency in javaeasiest way to find frequenucy of chars in javacode to find the frequency of a letter in a string in javacount the frequency of each letter in a string jabaget letter frequence javaget frequency of characte in string without loop javafind frequency of every word in string in javahow to count frequency of characters in javajava analyze char frequency in a stringfrequency count in string in javachar frequencyfrequency of letters in a string in javajava 8 count occurrences of char in stringcalculating frequency of elements of a stringcount the frequency of letters in a string javacounting character frequency in a string javajava character frequency arrayprogram to find frequency of characters in a string in javahow to count each letter foind in a stinghow to get frequency of a char in string javajava program to display the frequency of each character in a wordcount the frequency of t in javacalculate the frequency of each character in the string javaget the frequency of characters in string javafrequency of string char count frequency of characters in string javachar frequency javacount the frequencies of character in stringswrite a java program to find the frequency of characters in a user inputted string string start n characters to count javacounting frequency of characters in a string javafrequency of character in javafind the frequency of characters in a string in alphabetical order and return a string in javahow to calculate frqquency of string in javacount frequency of letters in javafrequency of letter code in javahow to find frequency of given characters in a string in javajava program to count frequency of any character in stringfind the frequency of a character in an array in javahow to compare frequencies of letters in a string c 23frequency of characters in a string in javaprint frequency of letters of a string in javafrequency of substring in a string in javaprint the frequency of characters in a string in javaletter occurence counter javacount the frequency of letters javacount letters in string javacharacter frequency counter javacharacter frequency in a string javato find the characters of given frequency in a string in javafind frequency of a character in aq string javacharacter frequency javajava count capital letters in stringcount the frequency of characters in string javafind the frequency of characters in a string javastring frequency string javafrequency of a character in a string in javahow to check frequency of character in string in javafrequency of character in a string in javacalculate the frequency of characters in a stringjava program to find frequency of characters in a stringfind frequencies of words in string javacount frequency of characters in javafrequency of each characters in a string in java without string arrayfind character frequency javafrequency of characters in a array of string in javacount frequency of characters in stringjava program to count the total number of charactersfrequency of substring in a string javafrequency count in javajava program to find frequency of each character in a stringjava string character frequencyfrequency function of characters in a string in javacounting against how many times a letter appears in a string javaletter frequency analysis javafrequency of a char in a string array javahow to print frequency of each character in a string in javafrequency of character in a string using collection javacount string frequency javacount frequency of each character in string javahow to find the frequency of character in string in javacount the frequency of charachter t in javafrequency of a character in a string javacount frequency of a character in a string javafind frequency of string in javacalculate frequency of all chracters in javawrite a program to find the frequency of each characters present in the stringcalculate frequency of each character in a stringfind frequencies of elements in string javastore frequency of charachters using hashhow to make a frequency array of characters in javaprint the characters in a string and its frequency in order of size javaprint frequency of each character in a stringthe frequency of a char in string javafrequency of each character in a string in javafind frequency in string javacount frequency of letters in string javacount frequency of string javahow to count the frequency of characters in a string in javacount frequency of a character in javacharacter in a string and its frequencycharacter frequency in string javahow to find frequency of a character in an array javajava count frequency of characters in a stringfinding frequency of characters in a string javafind the frequency of characters in a stringjava count frequency of characters in a string functioncount the frequency of character in a given stringfind frequency of char in java without arraycharacter and frequency of strings in java codecount frequency of elements in string javacode to check frequecny of characters in string in javacount frequency of characters java and print in order of frequencyjava frequency of characters in stringwrite java program to find the frequency of characters in a string this program counts the frequency of characters in a string 2c i e 2c which character is present how many times in the string find frequency of characters in a string using java8java code for frequency countjava class count frequency of char in stringfind the frequency of each character in a given string in javagiven a string find the frequencies of each of the characters in itfrequency of character in string javacalculate letter frequency javajava frequency of chracter in stringcode for the letter frequency in javastring frequency of characters in javajava program to find the frequency of characters in a stringfrequency of given string javacounting letter frequency in a string javacount frequency of all words in string javacount string letters in javstring char frequency in javafing frequency of letter of string in javaprogram to find the frequency of characters in a string in javaprogram to count the frequency of each charecter of a string in javajava calculate the frequency of a characer in a stringwrite a program to find the frequency of each character present in the string javascriptfrequency of each characters in a string in javawrite a java program to find the frequency of a given character in a string algorithmcheck frequency of character in a stringcount the occurrence of a character in a string in javaget frequency of letters javafind the frequency of characters in a string in javahow to calculate frequency of characters in a string in javacount each characters in string javacount the letters in a string javajava string frequency of character in stringhow to find frequency of occurence of a charecter in a string javajava count frequency length of characters in a stringfrequency of characters in string javahow to check the frequency of a char in a string and return a valuehow to find frequency of characters in a string in javacount the frequncy of each character javacount frequncy of characters javawrite a program to find the frequency of each characters present in the string javascripthow to check frequency of a char in string javafirst write a method that accepts a string as argument that computes the frequency of each character in the string how to find the frequency of a character in a given string in javajava frequency stringcompute frequency in javahow to find the frequency of a string in javajava count how many specific characters in a stringcount the frequency of each letter in a given stringalgorithm to count frequency of characters in stringfind frequency of characters in a string in javaeasy way to check frequency of letters in a string in c 23counting frequencies of charcters in stringprint the frequency of the stringcount frequency of words in string javacounting the frequencies of characters within a string javafind the frequency of characters in a string in java