Intro to Data Structures + Algo [Part 2]
Tags: javascript, beginners, algorithms, challenge
Published: 2020-08-10 03:38:09

---
title: Intro to Data Structures + Algo [Part 2]
published: true
description: 2nd part of the data algo series
tags: javascript, beginners, algorithms, challenge
cover_image: https://thumbs.gfycat.com/AgileFoolhardyJoey-small.gif
---

### **Intro**

Hey guys, it's another week for our coding challenges (Data Structures and Algorithms). Today we are going to be talking about Arrays which is one of the fundamentals Data Structures. However, in layman terms what is a data structure? It simply is things that we could built from scratch, for example an array. This is one of the most common topics in interviews, and you will usually see questions such as filter X elements from the array, reverse that other array or even start sorting this array.

The best way for you to solve an array based question is to simply be more knowledgeable about this topic. Moreover, knowing more about fundamentals such as operators, loops and recursion will help. Another key benefit is that it offers fast O(1) search if you know the index, but adding and removing an element from an array is slow because you cannot change the size of the array once it's created.

![hohoo](https://media.giphy.com/media/QqdyVT8H6uJ32/giphy.gif)
<figcaption>Hopefully, I do have your attention</figcaption>

<hr />

### **Examples**

Before I forget, the following array methods have specific time complexities that you should keep in mind. The push() is a constant O(1), lookup() is O(1), insert() is O(n) & delete() O(n). 

```js
const ratings = ["M", "A", "X", "F"]
//                0    1    2    3  

ratings.push("N") // O(1)

ratings.unshift("T") // O(n)

ratings.splice(2, 0, "potatoes"); // O(n/2) simplified to O(n)
```

Great, now let's go over what are static and dynamic arrays. First, static arrays are the ones fixed in size, this means you are to specify ahead of time the number of elements your array will hold (Seen this more on C++ and Java). A Dynamic array allows us to copy and rebuild the array at a new memory location. What you need to remember is that Dynamic arrays expands as you add more elements, so you don't need to determine the size ahead of time.

*Note*: You should treat string questions as an array question.

![wait..what!](https://media.giphy.com/media/lkdH8FmImcGoylv3t3/giphy.gif)
<figcaption>Strings are simply an array of characters.</figcaption>
<hr />

A few array coding questions to practice with:

- **[EASY]** Create a function that reverses a string. [**Solution**](https://repl.it/join/pdnigahy-mius00)
```js
const reverse = str =>{ // leaving this here to get you started

}

reverse("Pikachu loves his trainer")
// reniart sih sevol uhcakiP

```

- **[EASY-MED]** Given two arrays that are sorted can you merge them into one big array (still sorted). [**Solution**](https://repl.it/join/pdnigahy-mius00)

```js
const mergeSortedArrs = (arr1, arr2) => {

}

mergeSortedArrs([1,12,28,53], [4, 7, 22, 37])
// output => [1,4,7,12,22,28,37,53]

```

If you guys want to read a more amazing blog about this topic, please check on Sean posts.

{% link hhttps://dev.to/seanwelshbrown/find-the-first-duplicate-in-a-javascript-array-5da3 %}
<hr />



### **Conclusion**

We are just beginning to cover early on on Data Structures and so far we seen Big O and Arrays. We mentioned how dynamic arrays have to double up or expand our memory. Also how strings are considered array questions. It's recommended to go and cheat the Big O cheat sheet and read more the section regards array (interpret / understand). This will help you more by the time we get into Algorithms. 

![running](https://i.pinimg.com/originals/ba/73/d4/ba73d439fcf393a791abd101d587c58f.gif)
<figcaption>See you next time ~~~ Bye.</figcaption>