Let's say I have a string and I would like to count each letter's frequency and then sort the table by the frequency. Desired output of "hello larry" would be:
+--------+-----------+
| Letter | Occurence |
+--------+-----------+
| l | 3 |
| r | 2 |
| h | 1 |
| e | 1 |
| o | 1 |
| a | 1 |
| y | 1 |
+--------+-----------+
First I thought I'll deal with this easily using map having the letters as keys. This is really easy. However, map items don't have an order hence can't be sorted.
I guess I could deal with this using a structure:
type Letter struct {
Value string
Score int
}
type LetterList []Letter
However that brings bunch of other problems:
- I need to check if the Letter is not already present in LetterList because I can't use the letters as keys
- There's no direct way to sort them (using Int.sort() or so)
Using the structures just doesn't feel elegant at all. Is there a better solution?