I am trying to make a function in haskell, where a list is entered as a parameter and it returns or shows the list but the duplicates are eliminated, that is:
Enter [1,2,3,4,1,2]
and show [1,2,3,4]
. It can also be with characters, not necessarily with numbers.
Something like that:
deleteDuplicate :: [a] -> [a]
The Haskell module
Data.List
already has a function,nub
, which removes repeated elements from a list at timeO(n^2)
.A simple way to define this function is to take the head
x
of the list(x:xs)
, remove occurrences ofx
in the tailxs
(filtering out elements other thanx
using the functionfilter
), and recursively remove repeated elements.