I have worked on this without asking for help and figured out most of what to do. Unfortunately, this code keeps outputting the last letter in the string instead of the most common and I can't figure out why. Any suggestions?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
char mostPopular(const string & s){
char ret;
int max = 0;
int count = 0;
for (unsigned pos = 0; pos < s.size(); pos++) {
for (unsigned pos1 = 0; pos1 < s.size(); pos1++) {
if (s[pos] == s[pos1]) {
count++;
}
if (count > max) {
max = count;
ret = s[pos];
}
}
}
return ret;
}