Created
October 27, 2021 00:08
-
-
Save shritesh/e995db806daf89445d5106418be5e045 to your computer and use it in GitHub Desktop.
NashFP nub
This file contains hidden or 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
defmodule Nubnub do | |
@moduledoc """ | |
Documentation for `Nubnub`. | |
""" | |
@doc """ | |
## Examples | |
iex> Nubnub.nub([1, 2, 3, 1, 2, 3]) | |
[1, 2, 3] | |
""" | |
def nub(vals) do | |
{_, result} = | |
Enum.reduce(vals, {MapSet.new(), []}, fn elem, {ms, acc} -> | |
if MapSet.member?(ms, elem) do | |
{ms, acc} | |
else | |
{MapSet.put(ms, elem), [elem | acc]} | |
end | |
end) | |
Enum.reverse(result) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment