Skip to content

Instantly share code, notes, and snippets.

@funador
Last active August 3, 2018 22:31
Show Gist options
  • Save funador/4f05880f4766791c09377964e5f6fc3b to your computer and use it in GitHub Desktop.
Save funador/4f05880f4766791c09377964e5f6fc3b to your computer and use it in GitHub Desktop.
const nums = [2, 6, 3, 10]
const nums2 = [2, 4, 1, 2]
const duplicates4 = arr => {
// Start with an empty Object as a
// record keeper
const obj = {}
for (let i = 0; i < arr.length; i++) {
const num = arr[i]
// If we have already seen this number
// before it must be a duplicate!
if(obj[num]) {
return true
}
// We haven't seen it before so let's
// start tracking it
else {
obj[num] = true
}
}
// We looked at everything and no matches!
// There are no duplicates
return false
}
console.log(duplicates3(nums)) // false
console.log(duplicates3(nums2)) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment