I need to find the first duplicate number in array and then print index of this number in console. In my code below I reach the moment when code finds the duplicate but prints its index. In simple words, 2 is duplictaing in array but it's stands in first place so I need to print index "0".
var sameNum = [2, 4, 5, 2, 3, 5, 1, 2, 4];
var firstIndex = [];
for (var i = 0; i < sameNum.length; i++) {
for (var j = i; j < sameNum.length; j++) {
if (i != j && sameNum[i] == sameNum[j]) {
firstIndex = [i];
}
}
}
console.log(firstIndex);