Created
September 8, 2017 18:11
-
-
Save juanigallo/41ea286f83a0fd430c05f6a601c6283c to your computer and use it in GitHub Desktop.
Find duplicates in array.
This file contains 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
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] + " ")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How is this supposed to work? I don't understand the algorithm, and in practice it seems to fail: