Skip to content

Instantly share code, notes, and snippets.

@funador
Last active August 3, 2018 22:30
Show Gist options
  • Save funador/5c2e6d27f0ddaf615e480e30eb0f386e to your computer and use it in GitHub Desktop.
Save funador/5c2e6d27f0ddaf615e480e30eb0f386e to your computer and use it in GitHub Desktop.
const nums = [2, 6, 3, 10]
const nums2 = [2, 4, 1, 2]
const duplicates2 = arr => {
for (let i = 0; i < arr.length; i++) {
// Cut down on our work by not considering pairs
// we have already looked at
for (let j = i + 1; j < arr.length; j++) {
// We have a match!
if (arr[i] === arr[j]) {
return true
}
}
}
// We looked at everything and no matches!
// There are no duplicates
return false
}
console.log(duplicates2(nums)) // false
console.log(duplicates2(nums2)) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment