Skip to content

clojure.core/frequencies

View this page on ClojureDocs

Type: function Added: Clojure 1.2 Examples: 3 Runnable: 2
([coll])

Returns a map from distinct items in coll to the number of times they appear.

Examples

by pkolloch on . May have evaluation errors.
by pharcosyle on
by bfontaine on

Note by lispro06

(fn [coll]
   (let [gp (group-by identity coll)] 
      (zipmap (keys gp) (map #(count (second %)) gp))))

Note by danneu

(into {} (for [[k v] (group-by identity "abbbc")] [k (count v)]))

Note by ppsreejith

user=> (frequencies [3 6 2 6 8 7 'b 'c 3 5 3 4 7 6 'a])
{2 1, 3 3, 4 1, 5 1, 6 3, 7 2, 8 1, a 1, c 1, b 1}
user=> (frequencies [3 6 2 6 8 7 'b 'c])
{3 1, 6 2, 2 1, 8 1, 7 1, b 1, c 1}
;Note that the order of keys need not be in order of vector

Note by humorless

(defn frequencies-by
  "a generalized version of frequencies"
  [f coll]
  (let [gp (group-by f coll)]
    (zipmap (keys gp)  (map #(count (second %)) gp))))

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.