Reputation: 21
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
Upvotes: 0
Views: 90
Reputation: 386550
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
Upvotes: 1
Reputation: 443
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)
Upvotes: 0
Reputation: 26844
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);
Upvotes: 1