Skip to content

Instantly share code, notes, and snippets.

@shritesh
Created October 27, 2021 00:08
Show Gist options
  • Save shritesh/e995db806daf89445d5106418be5e045 to your computer and use it in GitHub Desktop.
Save shritesh/e995db806daf89445d5106418be5e045 to your computer and use it in GitHub Desktop.
NashFP nub
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