29 lines
806 B
Plaintext
29 lines
806 B
Plaintext
defmodule RC do
|
|
# hash table approach
|
|
def uniq1(list) do
|
|
Enum.reduce(list, HashSet.new, fn x, set -> Set.put(set, x) end)
|
|
|> Set.to_list
|
|
end
|
|
|
|
# Sort approach
|
|
def uniq2(list), do: Enum.sort(list) |> uniq2([])
|
|
|
|
defp uniq2([], uniq), do: Enum.reverse(uniq)
|
|
defp uniq2([h|t], uniq) when h==hd(uniq), do: uniq2(t, uniq)
|
|
defp uniq2([h|t], uniq) , do: uniq2(t, [h | uniq])
|
|
|
|
# Go through the list approach
|
|
def uniq3(list), do: uniq3(list, [])
|
|
|
|
defp uniq3([], uniq), do: Enum.reverse(uniq)
|
|
defp uniq3([h|t], uniq) do
|
|
if Enum.member?(uniq, h), do: uniq3(t, uniq), else: uniq3(t, [h | uniq])
|
|
end
|
|
end
|
|
|
|
list = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant']
|
|
IO.inspect Enum.uniq(list)
|
|
IO.inspect RC.uniq1(list)
|
|
IO.inspect RC.uniq2(list)
|
|
IO.inspect RC.uniq3(list)
|