-2

splice is time complexity O(n); I tried this version instead of splice: which has added space complexity but i think less time complexity.

 let arr = [0,1];

    arr[5] = 5;
     // i need to push numbers into this index only-not to the whole array
     // push wont work on a 1d array but will work on 2d array
    arr[5] = [[], 5];
    // now it is possible to push into this index
    arr[5].push(15,125,1035);
    // remove the unnecessary empty cell []
    arr[5].shift();
    
    console.log(arr) // result [0,1,[5,15,125,1035]]

So is this worse than splice, or better(in terms of time complexity)?

EDIT: this is a bad take of the answer given, my problem was i didn't understand why you couldn't push into an index of an array. when you try: arr = [1,2,3,4] and then arr[1].push(2.5); you would get an error since you try and push into a primitive(number and not an object/array). My mistake was that i thought JS just doesn't allow it.

Arush
  • 25
  • 8
  • Why did you say that push wont work on a 1d array? `let arr = [5]; arr.push(15,125,1035)`, it gives the same `[5,15,125,1035]`. – Gerardo Furtado Jan 29 '23 at 09:27
  • bad example, i will give you a better array version. Edited – Arush Jan 29 '23 at 09:30
  • why do you have an empty cell at first? – Nina Scholz Jan 29 '23 at 09:31
  • read the title. try and push into a specific 1d array index. not possible in js. only with splice. – Arush Jan 29 '23 at 09:35
  • 1
    Just do `arr[5] = [5]`, then you do `arr[5].push(15,125,1035);`. By the way, what you said above (*"try and push into a specific 1d array index. not possible in js. only with splice. "*) is completely incorrect. – Gerardo Furtado Jan 29 '23 at 09:50
  • try and do arr[0].push(3); it will give you an error. if the arr = [0,1] for example. – Arush Jan 29 '23 at 09:53
  • Obviously it will give you an error, because you can only `push` to **arrays**, not primitives. If `arr[0]` is a number, say `5`, what do you expect that `5.push(3)` will give? – Gerardo Furtado Jan 29 '23 at 09:56
  • I know, that was my way of circumventing this. your way is much simpler. i wasn't aware of this. – Arush Jan 29 '23 at 09:57

3 Answers3

0

If you want result [5,15,125,1035] with simply this way.

 let arr = [5];
     arr.push(15,125,1035);
    
    console.log(arr)
GMKHussain
  • 3,342
  • 1
  • 21
  • 19
0

Javascript leaves the underlying implementation of arrays up to the runtime, so the answer will depend on implementation used: How are JavaScript arrays implemented? but I will assume this array is entirely a flat array.

Pushing to the end of an flat array is only O(1) if the array has space for more elements. Otherwise the array needs to be reallocated which is O(length_of_array). However it should still be faster than splicing on every insertion.

If you want O(1) insertion speed then you could append to the end of a doubly linked list instead, however this is at the cost of space and lookup/iteration speed.

EDD
  • 2,070
  • 1
  • 10
  • 23
-1

the simpler answer i was looking for is by @Gerardo Furtado

let arr = [0,1,2,3,4,5];

    arr[5] = [5];
    arr[5].push(15,125,1035);
    
    console.log(arr) // result [0,1,[5,15,125,1035]]
Arush
  • 25
  • 8