Having some trouble with this code snippet. Tried running it but no luck. Here are the test cases:
- Input:
2,1,3,5,3,2
Expected Output: 3; - Input:
2,4,3,5,1
Expected Output: -1 - Input:
2,4,3,5,1,7
Check out the code below:
function FirstDuplicate(array) {
var a = [5, 2, 3, 4, 2, 6, 7, 1, 2, 3];
var firstDuplicate = "";
for (var i = 0; i < a.length; i++) {
for (var b = i + 1; b < a.length; b++) {
if (a[i] === a[b])
firstDuplicate = a.indexOf(a[i]);
break;
}
}
return firstDuplicate;
}