Regarding Max Frequency character problem


why this code is not giving correct output ???

Sorting and printing the last element will not do. Logic is wrong.
You are increasing the frequency at index where index is the ascii value of a character.
So if you sort the array, the frequency character which is attached to its index as ascii value will get disturbed. You can just dry run and check.
Instead of sorting…iterate through the whole frequency array to check the maximum frequency.

1 Like

The code is working as per the approach discussed… but is it the most optimal way to solve this question??

Yes hashing based approach to store the frequency of each character is an optimal way.