Clojure Goodness: Remove Duplicates From A Collection With distinct
With the function distinct
we can remove duplicate elements from a collection. The function returns a lazy sequence when we use a collection argument. Without arguments the function returns a transducer. When we want to remove duplicates and we don’t need the lazy sequence result we could also turn a collection into a set with for example the set
or into
functions.
In the following example we use the distinct
function on several collections.
(ns mrhaki.core.distinct
(:require [clojure.test :refer [is]]))
;; In the following example we have the results
;; from several throws with a dice and we want
;; to remove all duplicates.
(is (= [1 5 6 2 3] (distinct [1 5 5 6 2 3 3 1])))
;; Only duplicates are removed.
(is (= ["Clojure" "Groovy" "Java"]
(distinct ["Clojure" "Groovy" "Java" "Java" "Java" "Clojure"])))
;; String is also a collection we can invoke distinct function on.
(is (= [\a \b \c \d \e \f] (distinct "aabccdeff")))
;; For example a collection of mouse clicks where
;; we want to get rid of duplicate clicks at the same position.
(is (= [{:x 1 :y 1} {:x 1 :y 2} {:x 0 :y 0}]
(distinct '({:x 1 :y 1} {:x 1 :y 2} {:x 1 :y 1} {:x 0 :y 0}))))
;; When we don't need the sequence result with ordening we can
;; also use a set to remove duplicates.
;; We loose the order of the elements.
(is (= #{1 5 6 2 3}
(set [1 5 6 5 2 3 1])
(into #{} [1 5 6 5 2 3 1])))
Written with Clojure 1.10.1.