Skip to content

Instantly share code, notes, and snippets.

@funador
Last active August 3, 2018 22:31
Show Gist options
  • Save funador/edc51bf3dc946a91739bec9519ecc3d1 to your computer and use it in GitHub Desktop.
Save funador/edc51bf3dc946a91739bec9519ecc3d1 to your computer and use it in GitHub Desktop.
const nums = [2, 6, 3, 10]
const nums2 = [2, 4, 1, 2]
const duplicates3 = arr => {
for (let i = 0; i < arr.length; i++) {
const num = arr[i]
// We take a slice of the remaining Array
// and see if that includes the num
// it must be a duplicate
if(arr.slice(i + 1).includes(num)) {
return 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