Remove duplicate elements from a list in Erlang

And for those looking to preserve the order of the list:

remove_dups([])    -> [];
remove_dups([H|T]) -> [H | [X || X <- remove_dups(T), X /= H]].

You could use sets, for example:

my_nonDuplicate_list1() ->
    List = [1,1,2,3,4,5,5,6],
    Set = sets:from_list(List),
    sets:to_list(Set).

This returns [1,2,3,4,5], no more duplicates, but most likely not sorted.

Another possibility without the usage of sets would be:

my_nonDuplicate_list2() ->
    List = [1,1,2,3,4,5,5,6],
    lists:usort(List).

In this case it returns [1,2,3,4,5], no more duplicates and sorted.

Tags:

List

Erlang