string frequency of characters in java

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

showing results for - "string frequency of characters in java"
Mirko
14 Aug 2020
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}
Silvana
10 May 2019
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}  
Rashan
17 Nov 2019
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}
Filippo
18 Mar 2018
1String str = "AAAABBCCDD"; // A4B2C2D2
2        String result1 = "";
3        String nonDup = "";
4
5        for(int i=0; i < str.length(); i++)
6            if(!nonDup.contains(""+str.charAt(i)))
7                nonDup+= ""+str.charAt(i);
8
9        for( int j=0;j < nonDup.length(); j++) {
10            int count = 0;
11            for(int i=0; i < str.length(); i++) {
12                if(str.charAt(i) == nonDup.charAt(j))
13                    count++;
14            }
15            result1 +=nonDup.charAt(j)+"" + count;
16        }
17        System.out.println(result1); // A4B2C2D2
queries leading to this page
how to calculate frqquency of string in javahow to calculate frequency of characters in a string in javastore frequency of charachters using hashcount frequncy of characters javahow to compare frequencies of letters in a string c 23count frequency of elelmet in char array javajava program to count frequency of any character in stringcount frequency of characters java and print in order of frequencychar frequency javafind frequencies of words in string javacalculate frequency of all chracters in javafind frequency of each letter javacharacter frequency counter javafrequency of characters in a string in java hackerrankhow to find the frequency of a character in a given string in javacount frequency of a character in a string javafirst write a method that accepts a string as argument that computes the frequency of each character in the string frequency of characters in a string in javafind frequency of char in java without arrayfrequency of letters in english javastring frequency count in javawrite a java program to find the frequency of characters in a user inputted string frequency of each character in a string in javafrequency of letters in a string in javacharacter frequency in string javajava program to count the total number of characterschar frequencycount frequency of letters in string javajava frequency of chracter in stringfind the frequency of a character in an array in javastring char frequency in javahow to print frequency of each character in a string in javacode to find the frequency of a letter in a string in javacompute frequency in javacharacter frequency in a string javacount the number of words in a string javacalculate frequency of each character in a stringcount frequency of each element in an string in javajava count frequency length of characters in a stringcode to check frequecny of characters in string in javacheck frequency of character in string javafind the frequency of characters in a string in alphabetical order and return a string in javajava code for frequency counthow to print frequency of each character in a string in java without using string functionsfrequency of substring in a string in javajava program to find frequency of words in a stringcheck frequency of character in a stringhow to find frequency of given character in a string in javajava count frequency of characters in a string functioncalculate the frequency of characters in a stringcode for the letter frequency in javahow to find frequency of characters in a string in javacount frequency of a character in javacount frequency of elements in string javahow to check the frequency of a char in a string and return a valuecount the frequency of t in javacounting letter frequency in a string javajava program to count the total number of characters in a string while loopget the frequency of characters in string javacount frequency of each character in string javacount the frequency of character in a given stringreturn a frequency of array of characters javacount the frequency of characters in string javafinding frequency of characters in a string javajava analyze char frequency in a stringcounting against how many times a letter appears in a string javahow to find freq of each char in stringfrequency of letter code in javafrequency of characters in string javacharacter frequency javacount frequency of letters in javaget letter frequence javacount the frequency of each letter in a string jabafrequency of character in javafind the frequency of characters in a string javahow to find frequency of a character in an array javahow to find frequency of a character in a stringcount the frequncy of each character javaletter frequency counter javaprint the characters in a string and its frequency in order of size javajava program to count the frequency of each character in a stringjava program to find the frequency of a given character in a stringcount the frequency of letters javahow to find frequency of occurence of a charecter in a string javafind the frequency of each character in a given string in javacharacter frequency in javacounting character frequency in a string javaalgorithm to count frequency of characters in stringcount the frequency of charachter t in javajava program to display the frequency of each character in a wordfrequency of strings in javajava frequency of characters in stringeasiest way to find frequenucy of chars in javacharacter and frequency of strings in java codecounting frequencies of charcters in stringfind a frequency of character in phpfind frequency in string javafrequency of a character in a string in javafind frequencies of elements in string javacalculating frequency of elements of a stringjava count frequency of characters in a stringgiven a string find the frequencies of each of the characters in itfing frequency of letter of string in javafrequency of each characters in a string in java without string arrayhow to get frequency of a char in string javacounting frequency of characters in javaprogram to find frequency of characters in a string in javajava program to find frequency of each character in a stringwrite a java program to find the frequency of a given character in a string algorithmstring frequency of characters in javajava string frequency of character in stringcount frequency of all words in string javafrequency of a char in a string array javahow to check frequency of character in string in javastring start n characters to count javajava count capital letters in stringcalculate the frequency of each character in the string javaeasy way to check frequency of letters in a string in c 23to find the characters of given frequency in a string in javawrite a program to find the frequency of each characters present in the stringcount the frequency of each letter in a given stringjava application to determine the frequency of each letter in a word write 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 javahow to find frequency of a character in a stringjava 8 count occurrences of char in stringjava count how many specific characters in a stringsort a string according to the frequency of characters in javafind frequency of character in string javaprogram to find the frequency of characters in a string in javacounting frequency of characters in a string javacount the frequency of each character in a array in c 23java count occurences of specific characters in stringprint string with corresponding frequency count the letters in a string javahow to count frequency of characters in javafind character frequency javahow to find freequncy of charecters in a sting in javajava character frequency arraycount frequency of words in string javacount frequency of string javawrite a program to find the frequency of each characters present in the string javascriptcount frequency of characters in string javacount the frequencies of character in stringsget frequency of letters javastring frequency in javacharacter in a string and its frequencyfind the frequency of characters in a string using java streamsprint frequency of letters of a string in javaprogram to count the frequency of each charecter of a string in javafind the frequency of characters in a string using streamsfrequency of character in a string using collection javawrite a java program to count the frequency of words 2c characters in the given line of text java frequency stringprint frequency of each character in a stringfind the frequency of characters in a string in javafrequency of string char calculate letter frequency javafrequency of each characters in a string in javafrequecy funtion og strijng javacount the frequency of letters in a string javathe frequency of a char in string javafrequency count in string in javahow to find the frequency of character in string in javahow to count the frequency of characters in a string in javafind frequency of characters in a string using java8write a program to find and print the frequency of a letter in a string in javafrequency function of characters in a string in javacalculate frequency of characters in a string javacount string letters in javjava calculate the frequency of a characer in a stringcount each characters in string javafrequency of character in a string in javafrequency of characters in a string in java using mapletter occurence counter javafind frequency of every word in string in javahow to make a frequency array of characters in javafrequency of character in string javafind the frequency of characters in a stringcount frequency of characters in javacount string frequency javaletter frequency analysis javacount frequency of characters in stringfrequency of string in javaget frequency of characte in string without loop javafrequency of char in string javahow to count each letter foind in a stinghow to check frequency of a char in string javafrequency of a character in a string javahow to find the frequency of a string in javacount the occurrence of a character in a string in javajava program to find the frequency of characters in a stringjava string character frequencyprint the frequency of the stringfrequency of given string javafind frequency of string in javahow to find frequency of given characters in a string in javajava class count frequency of char in stringfind frequency of a character in aq string javacounting the frequencies of characters within a string javafrequency of substring in a string javajava program to find frequency of characters in a stringjava count the number of characters in a stringfrequency of characters in a array of string in javastring frequency javawrite a program to find the frequency of each character present in the string javascriptstring frequency string javafind frequency of characters in a string in javafrequency count in javacount letters in string javaprint the frequency of characters in a string in javacount the frequency of character in a given string javastring frequency of characters in java