0

I need to find the index of the first duplicated number in an array and assign it to an empty variable using only for loop

Thanks in advance

i have tried many logical operators.

var findIndex;
var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8]; //-----> it should give result console.log(findIndex) // 0

var arrWithNumbers = [3, 4, 5, 2, 6, 5, 1, 2, 4, 8]; //-----> it should give result console.log(findIndex) // 1


var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;
for (var i = 0; i < arrWithNumbers.length; i++) {
  if (arrWithNumbers[i] === i) {
    firstIndex = arrWithNumbers.indexOf(i);
    break;
  }
}
console.log(firstIndex);

what I expect:

var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8]; //-----> it should give result console.log(findIndex) // 0

var arrWithNumbers = [3, 4, 5, 2, 6, 5, 1, 2, 4, 8]; //-----> it should give result console.log(findIndex) // 1

//what i have
var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;
for (var i = 0; i < arrWithNumbers.length; i++) {
  if (arrWithNumbers[i] === i) {
    firstIndex = arrWithNumbers.indexOf(i);
    break;
  }
}
console.log(firstIndex); // 2

3
  • firstIndex = arrWithNumbers.indexOf(i); should be firstIndex = i; Commented May 3, 2019 at 10:04
  • Your if condition does not check for duplicates. Commented May 3, 2019 at 10:10
  • Hi Sudhir, unfortunately when i changed firstIndex = arrWithNumbers.indexOf(i); should be firstIndex = i it gives me the result 5. for array var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8]; i should have result 0, because 2 is the first duplicate at the position 0 in array :( Commented May 3, 2019 at 10:18

3 Answers 3

1

One option you have is to have a variable that contains all the count of the number, you can do this by using reduce

var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;
var numberCount = arrWithNumbers.reduce((c, v) => (c[v] = (c[v] || 0) + 1, c), {});


for (var i = 0; i < arrWithNumbers.length; i++) {
  if (numberCount[arrWithNumbers[i]] > 1) {
    firstIndex = i;
    break;
  }
}

console.log(firstIndex);


Another option is using lastIndexOf. If the current index is not the same as the lastIndexOf value, means that it has duplicate and break the loop.

var arrWithNumbers = [3, 2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;

for (var i = 0; i < arrWithNumbers.length; i++) {
  if (i !== arrWithNumbers.lastIndexOf(arrWithNumbers[i])) {
    firstIndex = i;
    break;
  }
}

console.log(firstIndex);

Sign up to request clarification or add additional context in comments.

Comments

1

You could take a hash table for visited values and store their indices. Then you need only the check if the hash property is set and return the index.

This approach works with a single loop and exits early on the first found same value.

function findIndex(array) {
    var indices = Object.create(null),
        i, value;

    for (i = 0; i < array.length; i++) {
        value = array[i];
        if (value in indices) return indices[value];
        indices[value] = i;
    }
}

console.log(findIndex([2, 4, 5, 2, 6, 5, 1, 2, 4, 8])); // 0
console.log(findIndex([3, 4, 5, 2, 6, 5, 1, 2, 4, 8])); // 2

Comments

0

You can use a nested for loop, to check all values after index i in your array :

var arrWithNumbers = [2, 4, 5, 2, 6, 5, 1, 2, 4, 8];
var firstIndex = null;
for (var i = 0; i < arrWithNumbers.length; i++) {
  value_i = arrWithNumbers[i]
  // loop through the next items of the array
  for (var j = i+1 ; j < arrWithNumbers.length; j++) {
    if (value_i == arrWithNumbers[j]) {
      firstIndex = i;
      break;
    }
  }
  
  if (firstIndex !== null) {
    // we found our firstIndex, quit the main loop
    break;
  }
  
}

console.log(firstIndex)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.