Skip to content

Instantly share code, notes, and snippets.

@albertywu
Created January 24, 2017 00:16
Show Gist options
  • Select an option

  • Save albertywu/19644834280a2ccb8b9f015ce58eea2b to your computer and use it in GitHub Desktop.

Select an option

Save albertywu/19644834280a2ccb8b9f015ce58eea2b to your computer and use it in GitHub Desktop.
// 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