6

I have two arrays

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

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

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

I have written a code but it works on array without duplicate`

var index = [];
for (i = 0; i <= arr2.length - 1; i++) {
  index.push(arr1.indexOf(arr2[i]));
}
2
  • What is newiter? My guess is that it is an array and from the code shown, it is one dimensional array. Remove [0] from it. Commented Feb 5, 2018 at 5:54
  • newiter is [ 0, 0, 1, 2, 2 ] and duplicate is [ 0, 1, 2, 0, 2 ] Commented Feb 5, 2018 at 6:01

5 Answers 5

3

You need to search after the first index for the second element and same for all repeating elements(for third after index). You can specify fromIndex argument in Array#indexOf method to start the search at 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);


Using Array#map method to generate the index array.

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

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

Comments

0

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

Comments

0

You can look through the array and map it onto anther array, setting the first instance to undefined. Obviously this doesnt work if undefined is a value you could want to search for.

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

2 Comments

(And if you can't modify array 2, it's easy enough to copy it)
if(ind) fails for ind = 0
0

If you have just 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);

Comments

0

What you can do is iterate over arr2 and save found index from arr1 in variable, and if element of arr2 is equal to previous element in arr2 then compare from saved index + 1, for this you can use 2nd parameter of 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);

Comments

Your Answer

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