Elixir Odd Occurrences In Array
defmodule OddOccurrencesInArray do
def odd_occurrences_in_array(a) do
a
|> Enum.reduce(%{}, fn v, count ->
if Map.has_key?(count, v), do: Map.delete(count, v), else: Map.put(count, v, 1)
end)
|> Map.keys()
|> List.first()
end
end
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.