Skip to content

Instantly share code, notes, and snippets.

@juanigallo
Created September 8, 2017 18:11
Show Gist options
  • Save juanigallo/41ea286f83a0fd430c05f6a601c6283c to your computer and use it in GitHub Desktop.
Save juanigallo/41ea286f83a0fd430c05f6a601c6283c to your computer and use it in GitHub Desktop.
Find duplicates in array.
function findDuplicates(arr) {
for (var i = 0; i < arr.length; i++){
if (arr[Math.abs(arr[i])] >= 0) {
arr[Math.abs(arr[i])] = -arr[Math.abs(arr[i])]
} else {
console.log(Math.abs(arr[i] + " "));
}
}
}
@rettops
Copy link

rettops commented Sep 10, 2017

How is this supposed to work? I don't understand the algorithm, and in practice it seems to fail:

findDuplicates([42,3,41]);
42
3
41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment