JavaScript Data Structures and Algorithms (Arrays, part 1)
Tags: datastructures
Published: 2021-04-08 02:46:58

---
title: JavaScript Data Structures and Algorithms (Arrays, part 1)
published: true
series: JavaScript Data Structures and Algorithms
description: Breaking down JS Data Structures and Algorithms so you can ace that technical interview
tags: datastructures
highlight: true
cover_image: https://images.pexels.com/photos/302820/pexels-photo-302820.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940
---

Hello! It's been a while since I last wrote. I've been in a slump and not coping appropriately. But that's another story. Today I feel better and I thought I'd write about some of the Data structures that you can expect to be asked about at technical interviews. This will be a lengthy series since I can't speak on any data structure without the algorithms available to access, insert, delete, and search through these structures. I will assume you have a base understanding of JavaScript and of Big O Notation if you are reading this, so bear with the rudimentary lexicon ahead. Let's dive right in.

As a quick note, I encourage you, the reader, to comment below if there is any misinformation in this post. This is the area I struggle with the most. After all, we are all here to learn from each other, and to document what we learn with these blog posts. Tl;dr Fact check me if necessary please.

**Arrays**
Arrays are probably the Data structure you will work with the most as you write your code. Arrays are a collection of values that are stored between square brackets. Which is to say, these values get allocated in memory. Each value in an array contains a numerical index in ascending order from first to last. As I mentioned before, memory for arrays is allocated, or pre-determined. Their size cannot be mutated without the appropriate algorithms (more on this in a bit). Also, *arrays are zero-indexed*, which is to say that the first index in an array is not 1, but rather 0. This is a basic caveat but crucial that we remember.

**Insertion**
JavaScript exposes methods for us to insert an element in an array. We can use `push(element)` to insert an element at the end of an array. JavaScript does not need to iterate through a whole array to find the last index, and because of this, `push()` has a linear time complexity, or O(1). Contrarywise we can also insert an element at the first index of an array, at index 0, using `unshift()`. This method also has linear time complexity, O(1). One more thing: these methods return the array with the remaining values after running the aforementioned methods.



```javascript
var arrayExample = [1, 2, 3, 4, 5]
arrayExample.push(6) //arrayExample = [1,2,3,4,5,6];
arrayExample.unshift(0) //arrayExample = [0,1,2,3,4,5,6];
```

**Deletion**
Deletion is as simple as Insertion is when it comes to arrays. To remove the last element of an array we can use the `pop()` method, and to remove the first element of an array we can use `shift()`. Like their insertion counterparts, these methods also have O(1) complexity, and return the original array minus the removed values.

```javascript
arrayExample.pop(); //arrayExample = [0,1,2,3,4,5];
arrayExample.shift(); //arrayExample = [1,2,3,4,5];
```

**Access**
To access an element of an array, we simply specify the index of the desired element. This process has linear complexity as well, O(1). Just remember what I mentioned about arrays being zero-indexed.

```javascript
var arrayExample = [1,2,3,4,5]
var desiredElement = arrayExample[2] //prints 3
```

**Iteration**
Indices are how we iterate through each element in an array. Expect to have to use some form of iteration at a technical interview. I am of course referring to the use of loops. This process' time complexity is directly correlated to the number  of elements in an array. Whether an array has 2 elements, or 200 elements, its time complexity is O(n), where n is the number of elements in an array. JavaScript exposes many ways to iterate through an array's elements, but all have the same time complexity.
I'm sure you already have one of these in mind already; the ever-so-common `for` loop. We initialize a variable `i`, and for every index in an array (beginning with 0), one by one, our program will run a given set of directions; an algorithm. Loops run for as long as they remain true, so be sure to provide a point where your loop can become false, lest it run forever and crash. 

```javascript
for(let i =0; i < arrayExample.length; i++) {
  //strut it out
} 
```
One way to prevent an infinite loop is to call variables by their indices one by one, using `for(in)`:
```javascript
for(var index in arrayExample) {
  //walk a mile
}
```
We can also specify variables of an array with `for(of)`:
```javascript
for(var element of arrayExample) {
  //serve it ancient city style
}
```
If you want to be more expressive with what your loop must run for each element, such as running a function at *each* element of an array, we can use `forEach()`:

```javascript
arrayExample.forEach(function(element, index) {
  //talk it out
  //babble on
  console.log(element[index]) //prints 1,2,3,4,5
});
```

Simple, no? Data Structures from here on out build on top of this information, so with a good foundation of knowledge, and plenty of tenacity, you are capable of mastering this branch of programmming. That's all for now. Next time I'll go a little deeper into array functions before moving on to the next data structure. So stay tuned! Peace!

Oh! One more thing. For a visual breakdown of data structures and algorithms, check out [VisuAlgo](https://visualgo.net/en). It provides animations for insert/delete/search/iterate for data structures, along with written explanations. It sure helped me a lot. K, Til next time!