find nth most frequently occuring character in a string in c

I have a string say “aaabbacccd”
here count[a]=4 count[b]=2 count[c]=3 and count[d]=1 .
Then i have to find character with nth largest frequency .
eg above the character with 3rd highest frequency is b (because 1<2<3<4) and count[b]=2 .
One way i thought was using a structure with two fields (character and frequency ) and then sorting based on frequency .
Are there some other efficient ways to solve this ?

Thanks in advance

If array is sorted then you can use binary search for counting the frequency of character by implementing fast and last occurrence of a character.

@abhik The array is not sorted and if we have to sort it (using any sorting technique) not only the counter array but the alphabets should also be swapped ie in above problem after soring it would be 1 2 3 4 but the corresponding alphabets must also be changed as d b c a
(just like sorting of struct objects ) :slight_smile: