Last active
August 3, 2018 22:31
-
-
Save funador/4f05880f4766791c09377964e5f6fc3b 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
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