String- MAx frequency problem----

what is the error in my code—

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
String str = cin.next();
Max(str);
}
public static void Max(String str){
int max = 0;
StringBuilder st = new StringBuilder("");
for(int i = 0;i<str.length();i++){
int count =0;
for(int j = 0;j<str.length();j++){
if(str.charAt(i) == str.charAt(j)){
count ++;
}
}
if(max<count){
max = count ;
st.append(str.charAt(i));
}
}
System.out.print(st);
}
}

Hi Abhishek,
In the question all you need to do is count the occurrence of every char and print the char which has the highest frequency. For that, you can use an integer array of size 26. Now traverse the string and for every char increment the corresponding position on array by 1. And after that you can find the max by traversing the array once again. And hence print the char.

is there any problem in my approach? is it complexty?

If you take the input aaabbbbc the output will be ab rather than just b. Also the complexity is O(n^2) which will give TLE for large test cases.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.