Skip to content

Instantly share code, notes, and snippets.

@i7an
Created June 28, 2017 10:03
Show Gist options
  • Save i7an/60b3c49a43d804f48f59dbdf51ef5eb7 to your computer and use it in GitHub Desktop.
Save i7an/60b3c49a43d804f48f59dbdf51ef5eb7 to your computer and use it in GitHub Desktop.
-module(main).
-export([nub/1, nub2/1, test/0]).
-import(lists, [reverse/1]).
nub(List) ->
Reversed = nub(List, sets:new(), []),
reverse(Reversed).
nub([], _, Result) -> Result;
nub([X|Xs], Set, Result) ->
case sets:is_element(X, Set) of
true -> nub(Xs, Set, Result);
false -> nub(Xs, sets:add_element(X, Set), [X|Result])
end.
nub2(List) ->
nub(reverse(List), sets:new(), []).
test() ->
[2,4,1,3] = nub([2,4,1,3,3,1]),
[2,4,3,1] = nub2([2,4,1,3,3,1]),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment