Skip to content

Instantly share code, notes, and snippets.

@tmartin8080
Last active February 9, 2023 12:46
Show Gist options
  • Select an option

  • Save tmartin8080/d8c0f9da74254532df3af7074a8c315f to your computer and use it in GitHub Desktop.

Select an option

Save tmartin8080/d8c0f9da74254532df3af7074a8c315f to your computer and use it in GitHub Desktop.
leetcode-two-sum-solution
defmodule Solution do
def two_sum(nums, target) do
nums
|> Enum.with_index()
|> Enum.reduce_while(%{}, fn {num, index}, acc ->
diff = target - num
if Map.has_key?(acc, diff) do
{:halt, [Map.get(acc, diff), index]}
else
{:cont, Map.put(acc, num, index)}
end
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment