Finding duplicate index in array in js

I have two arrays

arr1=[ 0, 1, 2, 0, 2 ];

arr2=[ 0, 0, 1, 2, 2 ];

I need to find the index of the elements arr2 from arr1, and the output array should be like [0,3,1,2,4];

I wrote the code , but it works with the array without duplicate`

var index = [];
for (i = 0; i <= arr2.length - 1; i++) {
  index.push(arr1.indexOf(arr2[i]));
}
+4
source share
5 answers

var arr1 = [ 0, 1, 2, 0, 2 ];
var arr2 = [ 0, 0, 1, 2, 2 ]
var index = [];
var hash = {};
for (i = 0; i < arr2.length; i++) {
  var ind_temp;
  if(arr2[i] in hash){
    //console.log("here");
    ind_temp = arr1.indexOf(arr2[i],hash[arr2[i]] + 1);
    index.push(ind_temp);
    hash[arr2[i]] = ind_temp;
  }
  else{
    ind_temp = arr1.indexOf(arr2[i]);
    index.push(ind_temp);
    hash[arr2[i]] = ind_temp;
  }
}

console.log(index);
Run code
0
source

You need to search after the first index for the second element and for all repeating elements (for the third after the index). You can specify an argument fromIndexin Array#indexOfto start a search on a specific index.

// use a reference object to specify from index for duplicate
var ref = {};

var index = [];

for (i = 0; i < arr2.length; i++) {
  // specify from index from object, if not found set as 0
  var i1 = arr1.indexOf(arr2[i], ref[arr2[i]] || 0);

  // push the index
  index.push(i1);

  // specify from index for current element
  ref[arr2[i]] = i1 + 1;
}

var ref = {};

var arr1 = [0, 1, 2, 0, 2],
  arr2 = [0, 0, 1, 2, 2];


var ref = {};

var index = [];

for (i = 0; i < arr2.length; i++) {
  var i1 = arr1.indexOf(arr2[i], ref[arr2[i]] || 0);
  index.push(i1);
  ref[arr2[i]] = i1 + 1;
}

console.log(index);
Run code

Using a Array#mapmethod to create an array of indices.

var index = arr2.map(function(v, i) {
  // get the index of the element, where specify from index to
  // search after a certain index for repeating element
  var i1 = arr1.indexOf(v, this[v] || 0);

  // set reference of index 
  this[v] = i1 + 1;

  // return index
  return i1;
  // set this argument as an object for from index reference
}, {});

var arr1 = [0, 1, 2, 0, 2],
  arr2 = [0, 0, 1, 2, 2];

var index = arr2.map(function(v, i) {
  var i1 = arr1.indexOf(v, this[v] || 0);
  this[v] = i1 + 1;
  return i1;
}, {});

console.log(index);
Run code
+3
source

, undefined. , , undefined - , .

var zz = arr1.map(val => {
  if (!val) return undefined
  let ind = arr2.indexOf(val)
  if (ind) arr2[ind] = undefined
  return ind
})
0

What you can do is iterate over arr2 and save the found index from arr1 in a variable, and if the element arr2 is equal to the previous element in arr2, then compare it with the saved index + 1, for this you can use the 2nd parameter of the indexOf method.

var duplicate =[ 0, 1, 2, 0, 2 ];
var newiter =[ 0, 0, 1, 2, 2 ];
var indexArray = []; //RESULT ARRAY

var newiter = newiter.sort(); //IN CASE newiter IS NOT SORTED

var i = -1;
for(var j = 0; j<newiter.length; j++) {

  // check if element from newiter is equal to previous , if not set i to -1
  if(j > 0 && newiter[j] != newiter[j-1]) {
    i = -1;
  }

  // get index from duplicate but start searching from i+1
  i = duplicate.indexOf(newiter[j], i+1);
  indexArray.push(i);
}

console.log(indexArray);
0
source

If you have only positive numbers, try this

var temp = arr1.slice(0); //Clone arr1 to a temp Arr

var index = [];

arr2.forEach(item => {
    let ind = temp.indexOf(item);
    index.push(ind);
    ind > -1 && (temp[ind] = -1);
})

console.log(index);
0
source

Source: https://habr.com/ru/post/1693174/


All Articles