ClojureDocs

Nav

Namespaces

distinct

clojure.core

Available since 1.0 (source)
  • (distinct)
  • (distinct coll)
Returns a lazy sequence of the elements of coll with duplicates removed.
Returns a stateful transducer when no collection is provided.
4 Examples
user=> (distinct [1 2 1 3 1 4 1 5])
(1 2 3 4 5)
user=> (def fractions 
         (for [n (range 1 100) d (range (inc n) 100)] 
           (let [gcd (clojure.contrib.math/gcd n d)] 
             (/ (/ n gcd) (/ d gcd)))))
;; all irreducible fractions with denominator < 100
;; (1/2 1/3 ... 1/99 2/3 1/2 2/5 1/3 ...)

user=> (count fractions)
4851

user=> (count (distinct fractions))
3003
user=> (distinct [1/2 2/4])
(1/2)

user=> (distinct [1/2 0.5])
(1/2 0.5)
user=> (apply str (distinct "tattoo"))
"tao"
See Also

Returns true if no two of the arguments are =

Added by ryo

Returns a lazy sequence removing consecutive duplicates in coll. Returns a transducer when no coll...

Added by blx

Returns a set of the distinct elements of coll.

Added by blx
3 Notes
    By , created 14.3 years ago

    If you do not need the lazyness of distinct, set can be faster. Like: (count (set some-coll)).

    By , created 11.0 years ago

    Use this function if you want to remove only consequtive duplicates

    (defn distinct-consequtive [sequence] (map first (partition-by identity sequence)))
    
    (distinct-consequtive [1 1 2 3 3 2 2 3])
    ;=> (1 2 3 2 3)
    
    By , created 8.6 years ago

    Use dedupe if you want to remove consecutive duplicates, available since Clojure 1.7.