Skip to content

Instantly share code, notes, and snippets.

@Ceroce
Created July 6, 2017 10:25
Show Gist options
  • Save Ceroce/17ea78688b11ed700143e6393f843c1c to your computer and use it in GitHub Desktop.
Save Ceroce/17ea78688b11ed700143e6393f843c1c to your computer and use it in GitHub Desktop.
The nub() function removes duplicates from a list
-module (nub).
-export ([nub/1, containsElement/2]).
% Remove duplicates in a list, keeping the order of the last occurences
-spec nub([T]) -> [T].
nub([]) -> [];
nub([X|Xs]) ->
case containsElement(Xs, X) of
true -> nub(Xs);
false -> [X|nub(Xs)]
end.
% Whether a list contains a particular element
containsElement([], _Elmt) ->
false;
containsElement([X|Xs], Elmt) ->
(X == Elmt) orelse containsElement(Xs, Elmt).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment