Code fails in last test cse

my code is failed in the last test …what is the problem with my code:
#include
#include
#include
using namespace std;
int main() {
string s;
cin>>s;
char ch;
int n=s.length();
sort(s.begin(),s.end());
int k=1,max=0;
for(int i=1;i<n;i++)
{
if(s[i]==s[i-1])
k++;
if(s[i]!=s[i-1])
{
if(k>max)
{
max=k;
ch=s[i-1];
}
k=1;
}
if(k>max)
{
ch=s[n-1];
}
}
cout<<ch<<endl;
}

hey @sawan_verma, ur code is printing some garbage value if the length if string is equal to 1.