mercredi 5 juillet 2017

printing index of duplicate numbers in array javascript

I want to find duplicate numbers in my array and then print it's index. I find the duplicate number but don't know how to print index instead of number. This is my code:

var sameNum = [1, 2 ,3 ,4 ,5 ,6 ,6 ,7 ,8 ,9];
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 = sameNum[i];
    }
  }
}
console.log(firstIndex);

I've tried to change sameNum[i] to indexOf.sameNum[i] but it doesn't work.Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire