Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,28 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue