This page describes an operation in the collection pipeline pattern. For more context read:


Removes duplicate elements

Returns a new list with any duplicates removed.

# => [1, 2, 3]
(distinct [1 2 3 2 1])
;; => (1 2 3)

Some collection frameworks have a separate Set class to handle sets. These automatically remove duplicates and do not have any ordering - which makes them a suitable model for mathematical sets. In these cases you can use a conversion operation to convert the list to a set to remove duplicates - but that won't be suitable if you want to preserve ordering. Other frameworks just use lists to represent sets, relying on the programmer to remove duplicates when needed.

Elements are usually compared using the equality operator. However some languages allow you to supply a lambda for the comparison.

["Bach", "Sibelius", "Beethoven", "Elgar", "Schumann"].
  uniq {|c| c[0]}
# => ["Bach", "Sibelius", "Elgar"]