Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove duplicates from a list in Clojure?

Tags:

list

clojure

How can I remove duplicate values from a list? For example,

(remove-duplicates ["a" "b" "c" "a"])
  => ("a" "b" "c")
like image 977
john Avatar asked Sep 18 '10 17:09

john


3 Answers

user=> (distinct '(34 56 45 34 56 89 11 4 11 78 11))
(34 56 45 89 11 4 78)
like image 188
missingfaktor Avatar answered Sep 21 '22 22:09

missingfaktor


If you don't care about the order, you can simply convert the list to a set:

user=> (set '("a" "b" "c" "a" "lala" "d"))
#{"a" "b" "c" "d" "lala"}
like image 28
sepp2k Avatar answered Sep 19 '22 22:09

sepp2k


Dedupe is the faster equivalent for sorted lists since dedupe only keeps the prior element in memory.

like image 23
John Asbaghi Avatar answered Sep 19 '22 22:09

John Asbaghi