Skip to content Skip to sidebar Skip to footer

Find The First Repeated Number In A Javascript Array

I need to find first two numbers and show index like: var arrWithNumbers = [2,5,5,2,3,5,1,2,4]; so the first repeated number is 2 so the variable firstIndex should have value 0. I

Solution 1:

You can use Array#indexOf method with the fromIndex argument.

var numbers = [7, 5, 7, 6, 6, 4, 9, 10, 2, 11];

// iterate upto the element just before the lastfor (var i = 0; i < numbers.length - 1; i++) {
  // check the index of next elementif (numbers.indexOf(numbers[i], i + 1) > -1) {
    // if element present log data and break the loopconsole.log("index:", i, "value: ", numbers[i]);
    break;
  }
}


UPDATE : Use an object to refer the index of element would make it far better.

var numbers = [7, 5, 7, 6, 6, 4, 9, 10, 2, 11],
  ref = {};

// iterate over the arrayfor (var i = 0; i < numbers.length; i++) {
  // check value already defined or notif (numbers[i] in ref) {
    // if defined then log data and brek loopconsole.log("index:", ref[numbers[i]], "value: ", numbers[i]);
    break;
  }
  // define the reference of the index
  ref[numbers[i]] = i;
}

Solution 2:

Many good answers.. One might also do this job quite functionally and efficiently as follows;

var arr = [2,5,5,2,3,5,1,2,4],
   frei = arr.findIndex((e,i,a) => a.slice(i+1).some(n => e === n)); // first repeating element indexconsole.log(frei)

If might turn out to be efficient since both .findIndex() and .some() functions will terminate as soon as the conditions are met.

Solution 3:

You could use two for loops an check every value against each value. If a duplicate value is found, the iteration stops.

This proposal uses a labeled statement for breaking the outer loop.

var numbers = [1, 3, 6, 7, 5, 7, 6, 6, 4, 9, 10, 2, 11],
    i, j;

outer: for (i = 0; i < numbers.length - 1; i++) {
    for (j = i + 1; j < numbers.length; j++) {
        if (numbers[i] === numbers[j]) {
            console.log('found', numbers[i], 'at index', i, 'and', j);
            break outer;
        }
    }
} 

Solution 4:

Move through each item and find if same item is found on different index, if so, it's duplicate and just save it to duplicate variable and break cycle

var numbers = [7, 5, 7, 6, 6, 4, 9, 10, 2, 11];
var duplicate = null;
for (var i = 0; i < numbers.length; i++) {
  if (numbers.indexOf(numbers[i]) !== i) {
    duplicate = numbers[i];
    break; // stop cycle 
  }
}
console.log(duplicate);

Solution 5:

var numbers = [7, 5, 7, 6, 6, 4, 9, 10, 2, 11];
var map = {};
    
for (var i = 0; i < numbers.length; i++) {
   if (map[numbers[i]] !== undefined) {
       console.log(map[numbers[i]]);
       break;
   } else {
       map[numbers[i]] = i;
   }
}

Okay so let's break this down. What we're doing here is creating a map of numbers to the index at which they first occur. So as we loop through the array of numbers, we check to see if it's in our map of numbers. If it is we've found it and return the value at that key in our map. Otherwise we add the number as a key in our map which points to the index at which it first occurred. The reason we use a map is that it is really fast O(1) so our overall runtime is O(n), which is the fastest you can do this on an unsorted array.

Post a Comment for "Find The First Repeated Number In A Javascript Array"