Last active
August 6, 2019 20:28
-
-
Save jerolan/fd552457d5224078c876b08b052bc583 to your computer and use it in GitHub Desktop.
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
| /** | |
| * TwoSum | |
| * Write a function that takes a list and outputs alist of numbers | |
| * belonging to the list that can be summed up to the first element of the list. | |
| * | |
| * Test Cases | |
| * > twoSum [17, 4, 5, 6, 10, 11, 4, -3, -5, 3, 15, 2, 7] | |
| * 6, 11, 10, 7, 15, 2 | |
| * | |
| * > twoSum [7, 6, 4, 1, 7, -2, 3, 12] | |
| * 6, 1, 4, 3 | |
| */ | |
| function twoSum(array) { | |
| const set = new Set(array); | |
| const one = array[0]; | |
| return array.filter(two => set.has(one - two)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Loved your approach.
A solution using only very common pieces of the language: