DEV Community 👩‍💻👨‍💻

Oluwanifemi Latunde
Oluwanifemi Latunde

Posted on

Sort Characters By Frequency

Day 5 of the #I4G10DaysOfCodeChallenge featured sorting a given string in decreasing order based on frequency of the characters. The frequency of a character is the number of times it appears in the string.

The idea was to sort the vector pairs in accordance with the frequency that was stored after storing each letter together with its frequency in a vector of pairs. The vector should then be printed in order.

The steps I took were as follows:

  • Return count of character in the string by declaring a count variable, assigning it to an empty dictionary and updating it as you loop through all the characters in the string.

  • Next was to sort the vector, according to the number of characters.

  • And finally, to print the sorted vector content.

Thank You.

Top comments (0)

Build Anything...


Use any Linode offering to create something for the DEV x Linode Hackathon 2022. A variety of prizes are up for grabs, inculding $1,000 USD. 👀

Join the Hackathon <-