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}
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}
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}
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