I'm trying to solve a simple challenge where I write a function that returns the first duplicate number in an array.
This is what I tried:
function duplicateNumber(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = arr.length; j >= 0; j--) {
if (arr[i] === arr[j]) {
var dup_num = arr[i]
}
}
}
return dup_num
}
It doesn't seem to be working. What am I doing wrong?
Just realized I'm also looping from end to beginning and beginning to end.
in the array = [3, 5, 6, 8, 5, 3]
the duplicate number should be 5
since it's duplicated before 3.