Data Structures(Arrays) — 1

const strings = ['a', 'b', 'c', 'd'];//push
strings.push('e'); //O(1)
strings.pop(); //O(1)
strings.unshift('x'); //O(n)
strings.splice(2,0,'alien'); //O(n/2) -> O(n)

For unshift, when the new element is added in the front of the array, all the elements have to be re-assigned.

ex. ‘a’ (strings[0] -> strings[1]), ‘b’(strings[1] -> strings[2])

This will happen for the every element in array. Hence the time complexity is O(n).

For splice, the assignment happens for the only half end part of the array. However, O(n/2) -> O(n). Thus time complexity will be O(n).


lookup — O(1)

push — O(1)

insert — O(n)

delete — O(n)