Search code examples
clojureduplicatessequence

Returning duplicates in a sequence


The best I could come up with was:

(defn dups [seq]
  (map (fn [[id freq]] id) 
       (filter (fn [[id freq]] (> freq 1))
               (frequencies seq))))

Is there a more concise way?


Solution

  • Use a list comprehension:

    (defn dups [seq]
      (for [[id freq] (frequencies seq)  ;; get the frequencies, destructure
            :when (> freq 1)]            ;; this is the filter condition
       id))                              ;; just need the id, not the frequency