Search code examples
listdictionarydata-structuresclojure

Convert a list of maps by the values of the maps [clojure]


I have a list filled with many maps (all of them have the same key), like this:

({:a 1} {:a 1} {:a 2} {:a 2} {:a 3} {:a 2})

I would like to convert it to a map that stores the occurrence of the value of each map. For exemple, the list above should return the following map:

{:1 2, :2 3, :3 1}

Any ideas on how can i do that?


Solution

  • (def m '({:a 1} {:a 1} {:a 2} {:a 2} {:a 3} {:a 2}))
    
    (frequencies (map :a m)) ;; => {1 2, 2 3, 3 1}
    

    Note the keys of the result are not keywords, as that would be an odd thing to do.