Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 856
Joe Snikeris Avatar asked Nov 08 '11 20:11

Joe Snikeris


People also ask

Can a sequence have duplicate values?

Sequences are guaranteed to generate sequential values (to the ending limit). More likely, something else is inserting values ahead of the sequence, so there are "duplicates" by the time the sequence reaches those values.

Can Oracle sequence generating duplicate values?

Oracle sequences are guaranteed not to generate duplicate values.

Does remove duplicates remove the first or second?

When duplicates are removed, the first occurrence of the value in the list is kept, but other identical values are deleted. Because you are permanently deleting data, it's a good idea to copy the original range of cells or table to another worksheet or workbook before removing duplicate values.


1 Answers

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
like image 59
Matt Fenwick Avatar answered Oct 10 '22 13:10

Matt Fenwick