Created
January 24, 2017 00:16
-
-
Save albertywu/19644834280a2ccb8b9f015ce58eea2b 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
| // assumes the passed-in array contains a single non-duplicate. Finds the non-duplicate. | |
| // O(3n) = O(n) runtime | |
| findNonduplicate = (numbers) => { | |
| const count = {} | |
| // N | |
| numbers.forEach(n => count[n] = count[n] ? count[n] + 1 : 1) | |
| return (Object | |
| .keys(count) | |
| .map(n => [n, count[n]]) // N | |
| .find(([n, count]) => count === 1) // N | |
| )[0] | |
| } | |
| const numbers = [1, 2, 1, 2, 5, 4, 6, 3, 3, 4, 5] | |
| console.log(findNonduplicate(numbers)) // 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment