Category Archives: Array

HOW IS AN ARRAY DIFFERENT FROM LINKED LIST?

How is an Array different from Linked List?

The size of the arrays is fixed, linked lists are dynamic in size.

Inserting and deleting a new element in an array of elements is expensive, whereas both insertion and deletion can easily be done in Linked Lists.

Random access is not allowed in linked list.

Extra memory space for a pointer is required with each element of the linked list.

Arrays have better cache locality that can make a pretty big difference in performance.

 
 
 
 
 
 
 
 
 
 

HOW DO YOU ADD AN ELEMENT AT THE BEGINING OF AN ARRAY? HOW DO YOU ADD ONE AT THE END?

How do you add an element at the begining of an array? How do you add one at the end?

var myArray = [‘a’, ‘b’, ‘c’, ‘d’];
myArray.push(‘end’);
myArray.unshift(‘start’);
console.log(myArray); // [“start”, “a”, “b”, “c”, “d”, “end”]
With ES6, one can use the spread operator:

myArray = [‘start’, …myArray];
myArray = […myArray, ‘end’];
Or, in short:

myArray = [‘start’, …myArray, ‘end’];