Skip to content

Instantly share code, notes, and snippets.

@barsukov
Created December 27, 2016 14:30
Show Gist options
  • Save barsukov/b762f743e3adb145256b6ae140d54541 to your computer and use it in GitHub Desktop.
Save barsukov/b762f743e3adb145256b6ae140d54541 to your computer and use it in GitHub Desktop.
Hacker rank sock-merchant solution for elixir.
https://www.hackerrank.com/challenges/sock-merchant
defmodule Solution do
def duplicate_socks() do
read = IO.read :all
[_ | result] = String.split(read, ~r/\W/, trim: true)
#IO.write(:stdio, result)
accum = find_duplication(result, %{})
result = Enum.reduce(Map.keys(accum), 0, fn(key, acc) ->
acc = acc + div(accum[key] , 2)
end)
IO.write(:stdio, result)
end
def find_duplication([head | tail], accumulator) do
accumulator = if Map.has_key?(accumulator, head) do
acc = accumulator[head] + 1
Map.put(accumulator, head, acc)
else
Map.put(accumulator, head, 1)
end
find_duplication(tail, accumulator)
end
def find_duplication([], accumulator) do
accumulator
end
end
Solution.duplicate_socks()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment