Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: count characters - sort by frequency

package main

import (
	"fmt"
	"sort"
)

func main() {
	text := "This is a very long text. OK, maybe it is not that long after all."
	counter := make(map[string]int)
	for _, c := range text {
		//fmt.Printf("%v", string(c))
		if string(c) != " " {
			counter[string(c)]++
		}
	}

	fmt.Println(text)
	chars := make([]string, 0, len(counter))
	for chr := range counter {
		chars = append(chars, chr)
	}

	sort.Slice(chars, func(i, j int) bool {
		return counter[chars[i]] > counter[chars[j]]
	})
	for _, chr := range chars {
		fmt.Printf("%v: %v\n", chr, counter[chr])
	}

}