Count number of times a letter is in a word

前端 未结 1 1180
无人共我
无人共我 2021-01-22 19:37

I have the word describe, and I want to see how many times each letter appears in the word. Eg \"e\" appears twice, \"d\" appears once etc

I have tried

相关标签:
1条回答
  • 2021-01-22 19:52

    You can use frequencies to count the frequency at which each character appears in the string, returning a map like this:

    (frequencies "ababacdefg")
    => {\a 3, \b 2, \c 1, \d 1, \e 1, \f 1, \g 1}
    

    This works because the string is being treated as a sequence of characters. frequencies can be used on general collections:

    (frequencies [1 1 2 3])
    => {1 2, 2 1, 3 1}
    

    The key is the value being counted, and the value is the frequency.

    0 讨论(0)
提交回复
热议问题