Skip to content

Instantly share code, notes, and snippets.

@funador
Last active August 3, 2018 22:38
Show Gist options
  • Save funador/0d1178daed38b91313b930292a0d4f4e to your computer and use it in GitHub Desktop.
Save funador/0d1178daed38b91313b930292a0d4f4e to your computer and use it in GitHub Desktop.
const nums = [2, 6, 3, 10]
const nums2 = [2, 4, 1, 2]
const duplicates = arr => {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
// If the indexes do not match but the
// values do, we have a duplicate!
if (j !== i && arr[i] === arr[j]) {
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