Data Structures: An overview
Let’s start with data structures since we work with them everyday and it builds into algorithms.
To understand data structures, we need to first understand what data is.
Data refers to any piece of information that is stored or transmitted in digital form. It can be
anything from text, images, audio, video, or even something as simple as binary code.
These bits of data are the lifeblood of the internet, powering everything on the web.
As developers, we work with data everyday and have to find ways to store, retrieve, manipulate
and present this data in the applications we build.
Databases are what we use to store data for the long term, but data structures are common
formats that the data comes in.
Important: Different data structures have different quirks on how they work, with their own
advantages and disadvantages.
To understand this, instead of data, imagine you had different clothes that needed to be stored
and organized. We could store them in different boxes for summer, fall and winter.
We could get more granular and store them in different drawers for tops, bottoms, and coats.
Depending on the clothing type, our storage would have to be able to accommodate.
Storage for coats might be a closet with hangers, t-shirts might go in a small drawer, and pants
in a tall cabinet.
All these different types of storage have unique features similar to the different types of data
structures.
There are many different types of data structures such as:
Arrays
Objects (dictionaries if you’re coming from other programming languages)
Linked lists
Stacks
Queues
Trees, and
Graphs
Each data structure has different features about how data is stored and accessed from them, as
well as some additional features they can perform which give them different advantages and
disadvantages.
Let’s take a look at arrays and objects to better understand some of these advantages and
disadvantages!
Arrays
If you’ve ever written any code before, you’ve most likely encountered an array.
Arrays are the most well-known and widely used data structure in computer science. An array is
a collection of elements stored contiguously.
These elements can be of any data type, meaning they can store integers, floats, strings,
booleans and even other arrays!
Think of an array as a connected series of boxes, each of which can hold a value. Every box also
has an index number, which we use to access whats in that box.
The first box is given an index of 0, and it increases by 1 for each subsequent box (this is called
zero-based indexing).
If we wanted to access the third value ‘c’ in our myArr we would call myArr[2].
If we wanted to access the value ‘e’ we would call myArr[4].
Accessing a value from the array using the index takes O(1) time (constant time) which is as
efficient as it gets.
The problem is that this requires us to know which index the value we want is under. Unless
we’ve sorted the array in a way where we know what value maps to what index, we’re going to
have to manually find the value in the array.
Maintaining a separate map of indexes to values is also inefficient if those values move around
or if we add or remove values from the array.
Despite this, arrays are still incredibly useful and common. We’ll explore more about arrays in
algorithms when we talk about sorting and searching.
Objects
An object is a data structure that stores a collection of key-value pairs. Each key in the object
maps to a corresponding value, much like how a word in a dictionary maps to its definition.
Unlike arrays, which use an index number to access its values, dictionaries use keys to access
their values.
Also, unlike arrays, where the index is always an integer, the keys in an object can be strings and
integers but they can be any hashable object in some programming languages.
Finally, the values can be of any data type, including integers, strings, arrays, or even other
dictionaries.
Here is an object that maps the names of some fruits to their corresponding colours.
We can access the value of a specific key by calling the key.
For example
myObj[”apple”] would return us “red”, whereas myObj[”banana”] would give us “yellow”.
Retrieving values here runs in constant time as well!
Objects are the most common data structures because we store a lot of data as objects.
In frontend web development, almost all data is in JSON which stands for Javascript Object
Notation. The reason it’s so useful is it’s intuitive to use.
If we’re storing user profiles, we can use keys are the attributes and the values as the details.
If we had multiple users, we commonly see them stored in arrays: As mentioned earlier, if we
wanted to find Mo, unless we knew that he was at index 1, we would have to search through
every user object in the array to find him.
Using an object instead, we could store users with their firstname as the key:
Now, we can find any user with just their first name. If we wanted Mo, we could call
userProfiles[”Mo”] and we’ll get his user object!
tl;dr
There's a time and place to use arrays or objects to store your data, and each have their own
advantages and disadvantages. It’s up to us as developers to understand the problem we’re
trying to solve and pick the correct data structure.
Now, the remaining data structures are outside of the scope of this article, but you can learn
more about them in-depth in our Data Structures + Algorithms course.