Last active
February 9, 2023 12:46
-
-
Save tmartin8080/d8c0f9da74254532df3af7074a8c315f to your computer and use it in GitHub Desktop.
leetcode-two-sum-solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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