Create a max heap taking freq as the comparison point Now the first 5 elements will be the 5 most common Time complexity O(nlogn)
Sigiloso
Create a hash with characters ('a', 'b', etc.) as keys, and their frequencies as values. As you traverse the list increment the value in the hash for the given character, or instantiate it if the key doesn't exist. Also, keep pointers (or an array) of the 5 highest frequency characters, and after incrementing the value in the hash for a given character, check to see if it exceeds any of the 5 highest frequency characters, and if it does, update the 5 pointers/array. This gives you complexity of O(n), where n is the length of the list of characters.