Nie Selam
Nie Selam

Reputation: 1451

insert missing numbers into an array

I have the following two arrays, which is always pre-sorted:

[1,2,4,5,8]
[x,x,x,x,x]

I need to insert the missing elements and in corresponding array, put y so the output can be:

[1,2,3,4,5,6,7,8]
[x,x,y,x,x,y,y,x]

The data arrives separately but they always match size wise.

I have tried the following but am pretty sure i have over-complicated it.

function action(numbers,data){
  var len=numbers.length;
  if (len<=1){
    return [numbers,data];
  }
  var new_data=[] //stores new data
  var new_number=[] //stores new numbers
  for(var i=1;i<len;i++){
    var diff=numbers[i] - numbers[i-1];

    if(diff>1){
      //there is gap here
      var val=0;
      diff--;

      for(var j=0;j<diff;j++){
        val=numbers[i-1] + j +1;
        new_number.push(val)
        new_data.push('y')
      }
      //put current info after missing data was inserted
      new_number.push(numbers[i])
      new_data.push(data[i])
    }
  }

  //adjust first entry
  new_number.unshift(numbers[0])
  new_data.unshift(data[0])

  return [new_number,new_data];
}

It is not consistent and I myself can't follow it.

action([2002,2005,2007],['x','x','x']) =>[2002,2003,2004,2005,2006,2007], [x,y,y,x,y,x]

But the following is error:

action([2002,2003,2007],['x','x','x']) =>[2002,2004,2005,2006,2007], [x,y,y,y,x]

the output should have been 2002,2003,2004,2005,2006,2007 and x,x,y,y,y,x

Update

Adding an else to the diff>1 seems to fix the above errors but the solution is not elegant at all:

} else{
  new_number.push(numbers[i])
  new_data.push(data[i])
}

Upvotes: 2

Views: 1541

Answers (4)

HMR
HMR

Reputation: 39320

You could use forEach:

const demo = (numbers,xy)=>{
  let expected = numbers[0],retNumbers=[],retXy=[];
  numbers.forEach(
    (current,index)=>{
      while(current>expected){
        retNumbers.push(expected);
        retXy.push("y");
        expected++
      }
      retNumbers.push(current);
      retXy.push(xy[index]);
      expected++;
    }
  );
  return [retNumbers,retXy];
}

console.log(demo(
  [1,2,4,5,8],
  ["a","b","c","d","e"]
))

Upvotes: 1

G Cadogan
G Cadogan

Reputation: 182

You were so close. I only had to move a few lines. The original numbers needed pushed to the new array every time, not just when the difference is too large.

function tst(numbers, data){	
    var len=numbers.length;
    if (len<=1){
        return [numbers,data];
    }
    var new_data=[] //stores new data
    var new_number=[] //stores new numbers
    for(var i=1;i<len;i++){
        var diff=numbers[i] - numbers[i-1];

        if(diff>1){
            //there is gap here
            var val=0;
            diff--;

            for(var j=0;j<diff;j++){
                val=numbers[i-1] + j +1;
                new_number.push(val)
                new_data.push('y')
            }
        }
		
        //put current info after missing data was inserted
        new_number.push(numbers[i])
        new_data.push(data[i])
    }

    //adjust first entry
    new_number.unshift(numbers[0])
    new_data.unshift(data[0])

    return [new_number,new_data];
}
	
console.log(tst([1,2,4,5,8],['x','x','x','x','x']));

Upvotes: 2

Nenad Vracar
Nenad Vracar

Reputation: 122087

You could use while loop and start from last element and finish at first.

var a = [1, 2, 4, 5, 8]
var b = ["x", "x", "x", "x", "x"];
var i = a[a.length - 1];

while (i-- > a[0]) {
  if (!a.includes(i)) {
    var index = a.indexOf(i + 1)
    a.splice(index, 0, i);
    b.splice(index, 0, "y");
  }
}

console.log(a);
console.log(b);

Upvotes: 2

Vignesh Raja
Vignesh Raja

Reputation: 8761

Hope this helps. Worked-out with dummy arrays.

var a = [1,4,6,7,8,12,14,55];
var b = [2,3,5,9,13,25,27,54,56];
b.forEach(function(value){
    var i = a.findIndex(function(oldvalue){return oldvalue>value;});
    i==-1 ? a.push(value) : a.splice(i,0,value);;
});
console.log(a)

Upvotes: 0

Related Questions