Created
June 28, 2017 10:03
-
-
Save i7an/60b3c49a43d804f48f59dbdf51ef5eb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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