0

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);

1

7 Answers 7

2

If I'm understanding the question correctly (you're looking for the first index of a number that occurs more than once), then the value of i is what you're looking for as that represents the index.

if(i != j && sameNum[i] == sameNum[j]) {
  firstIndex.push(i);
  // or print it //
  console.log(i)
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes, that's what i'm looking for but i need to print index not number behind index
I added an example to your answer, does that make sense? You can also refactor it to collect values of j if you need all of the index locations of duplicates.
0

I have used this solution

Array.prototype.getDuplicates = function () {
    var duplicates = {};
    for (var i = 0; i < this.length; i++) {
        if(duplicates.hasOwnProperty(this[i])) {
            duplicates[this[i]].push(i);
        } else if (this.lastIndexOf(this[i]) !== i) {
            duplicates[this[i]] = [i];
        }
    }

    return duplicates;
};

console.log([1, 2 ,3 ,4 ,5 ,6 ,6 ,7 ,8 ,9].getDuplicates());

Comments

0

I would associative array for (fast) lookup/storing encountered values.

var inputArray = [1, 2 ,3 ,4 ,5 ,6 ,6 ,7 ,8 ,9];
var encounteredIndices = {};

for(var i = 0; i < inputArray.length; i++)
  if (encounteredIndices[inputArray[i]])
    console.log(i); // Or add to some array if you wish
  else
    encounteredIndices[inputArray[i]] = 1;

Comments

0

You can first sort the array if required.On doing this it will in ascending order.Then use forEach to loop over the array, and get the index

var sameNum = [1, 3, 2, 4, 5, 6, 6, 6, 7, 7, 7, 8, 9];
var firstIndex = [];
// sorting the array and iterating over it
// forEach array method accepts three parameter of which index is one of the parameter
sameNum.sort().forEach(function(item, index) {
  if (sameNum[index + 1] == sameNum[index]) {
    console.log(index + 1);
  }
})

Comments

0

Try this one:-

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<input id="input" type="time">
<button onclick="live()">run</button>
<!---->
<p id="output">Output</p>
<script>
       function live() {


  var sameNum = [1, 2 ,3 ,4 ,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 = i;
        }
        if (firstIndex != null){break;}
      }
  }

if (firstIndex == null){
  alert("No repeating element");
}
else{
  alert(firstIndex);
}
       }
</script>
</body>
</html>

Comments

0

Nested loops, in this case, are a bad solution. It gives O(n^2) complexity, where we can solve linearly with a map.

We can use a map to track letters that showed up so far in a single loop:

var sameNum = [1, 2 ,3 ,4 ,5 ,6 ,6 ,7 ,8 ,9];
var lettersMap = {};
var firstIndex = -1;

for (var i = 0; i < sameNum.length; i++) {
  if (lettersMap[sameNum[i]]) {
    firstIndex = i;
    break;
  }
  else {
    lettersMap[sameNum[i]] = true;
  }
};

console.log(firstIndex);

Comments

0

You can use the "j" to get the index of duplicate values, like this

var sameNum = [1, 3, 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]) {
      console.log("--------"+sameNum[i]+"--------");
      console.log("Index: "+ j);
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.