DS With JS — Arrays

Arrays or Lists are organized sequentially. They’re stored in contiguous memory. If you need to store some data and iterate over it, arrays are the best choice, especially if you know the indices.
Static & Dynamic Arrays
Static arrays are fixed in size. We need to specify the number of elements in the array while declaring it.
If we want to declare an array of groceries of 7 items, 7 consecutive addresses will be allocated in memory. If we realize later that we need to add one more item we will have to have dynamic arrays.
Dynamic arrays allow us to copy and rebuild an array at a new location with more memory (if current memory cannot accommodate the new items). If we want to add the 8th item to the grocery list, the entire list of 7 items is copied to a new location, allocate 14 blocks of memory (usually twice the current size), and add the 8th item in the new location.
Dynamic Arrays in JS
In JavaScript the lists or arrays are dynamic. They automatically allocate memory with the increase in the size of arrays.
Dynamic Arrays can have O(N) complexity!
const things = ['Books', 'Car', 'Home', 'Money'];things.push('Boat'); // 5
things.push('Goat'); // 6It is going to push 'Boat'and 'Goat' at the end of the array. But if the memory allocated for this array doesn't have space for the 5th item, it will allocate new memory with more space (usually doubles the existing memory), and iterate through the array to allocate memory to each item. In these scenarios, the complexity will not be O(1) but O(N).
But this is a minor detail and we might not have to worry about memory allocations or the possibility of copying the array to a new location.
Append — O(1)*
*Can be O(N)
We can assume that the push command (for dynamic arrays) is O(1)constant in time, but once in a while, we can encounter a situation when it is linear in time.
console.log(things); // ['Books', 'Dog', 'Home', 'Money']Array Implementation in JS
In this section let us implement Array in JavaScript. The implementation is for viewing the complexity.
But before we get into the operations I recommend brushing up the most basic operations on Arrays in JavaScript.
We will be using the following boilerplate code to implement arrays. You can try implementing the operations — lookup() or get(), push(), pop(), and delete().
