1

Could you please help me figure out this algorithm without using splice:

Write a program that inserts a new number A at an index B. For example, if array = [1, 3, 5, 7] and A = 10 and B = 2, by the end of your program, array should be [1, 3, 10, 5, 7].

My thinking is that I would loop through the array, and then replace A with B, but it's not quite right:

for(var i = 0; i < arr.length; i++) {
    arr[2] = 10;
}
RobG
  • 142,382
  • 31
  • 172
  • 209
Rory Perro
  • 441
  • 2
  • 7
  • 16
  • 4
    Posible duplicate of http://stackoverflow.com/questions/586182/how-do-i-insert-an-item-into-an-array-at-a-specific-index, what you need is the method `splice` of the `Array` object, check the answer for that question – higuaro Aug 20 '15 at 00:20
  • With or without using of splice()? :) – sinisake Aug 20 '15 at 00:33
  • 1
    without using splice – Rory Perro Aug 20 '15 at 00:36
  • *"but it's not quite right"* Do you know what is not right? What specifically are you having problems with? – Felix Kling Aug 20 '15 at 00:51
  • hint: you must insert old value from specified index, too! hint2: final array length is changed – sinisake Aug 20 '15 at 00:55
  • @RoryPerro could you insert that into your OP? (Also, you should *really* use splice if you can). – Qix - MONICA WAS MISTREATED Aug 20 '15 at 01:19
  • @RoryPerro it's not quite right as you overwrite the original element at arr[2] with your new value..a normal practice for doing this is to loop FROM THE END of the array, shift each of the element to right until the position you want to insert the new value, and insert it – shole Aug 20 '15 at 03:24

2 Answers2

2

Without using .splice you still have quite a few options. Let's look at a single, simple example for now.

This solution involves using a reverse loop, and adjusting our indices manually to insert a single value. This alters the original array.

You can see here we are just shifting our next values into our current position, which starts at the index that is equivalent to our length. Then we can slot our new value in the final index.

function insert (array, index, value) {
  var i = array.length;
  
  while (i > index) {
    array[i] = array[--i]; // Prefix is important.
  }
  
  array[i] = value; // Or array[index], i === index at this point.
  
  return array;
}

console.log(insert([1,2,3,4], 2, 'B'));

This only inserts a single element into the array. Can you figure out how to insert multiple elements starting from the index?


If you are allowed to use .slice and .concat you can emulate an inserting .splice. This returns a new array.

function insert (array, index, value) {
  return array.slice(0, index).concat(value, array.slice(index));
}

console.log(insert([1,2,3,4], 2, 'B'));
Oka
  • 23,367
  • 6
  • 42
  • 53
  • 1
    Presumably if *slice* and *concat* were available, then so would *splice*. ;-) – RobG Aug 20 '15 at 01:56
  • It's an important note to make. Sometimes bending the rules is part of the solution. – Oka Aug 20 '15 at 01:57
1

Since inserting affects the length of the array, one approach is to start moving elements from the end to their current position + 1, then when the required index is reached, insert the new element.

Using a decrementing for loop:

function slowSplice(array, element, index) {
  for (var i=array.length; i>index; i--) {
    array[i] = array[i-1];
  }
  array[index] = element;
}

Using a decrementing while loop:

function slowSplice2(array, element, index) {
  var i = array.length;
  while (i > index) {
    array[i] = array[--i];
  }
  array[index] = element;
}

Note that the above assumes a contiguous array. If sparse arrays must be accommodated, more work is required. If the function is required to be implemented as a general function, the splice algorithm from ECMA-262 should be followed.

RobG
  • 142,382
  • 31
  • 172
  • 209