← Data structures from scratch · Lesson 1/32
What is a data structure?
Arranging things to find them
A data structure is a way of arranging information so that certain operations are cheap. It's not just where the data lives: it's how fast you can find, add, remove or walk through what's in there.
You already use them without the name:
- The line at the tortilla shop: first in, first out. That's a queue.
- A stack of plates: the last one you put on top is the first one you take. That's a stack.
- A book's index, sorted A to Z: you don't read all of it to find a word. That's close to a search tree.
Choosing a structure means deciding which operations will be fast and which ones you'll pay dearly for.
The whole lesson on one page
Arranging things to find them
A data structure is a way of arranging information so that certain operations are cheap. It's not just where the data lives: it's how fast you can find, add, remove or walk through what's in there.
You already use them without the name:
- The line at the tortilla shop: first in, first out. That's a queue.
- A stack of plates: the last one you put on top is the first one you take. That's a stack.
- A book's index, sorted A to Z: you don't read all of it to find a word. That's close to a search tree.
Choosing a structure means deciding which operations will be fast and which ones you'll pay dearly for.
Why JavaScript
This course uses JavaScript for three reasons:
- It runs right here, in your browser: every example has a RUN button, and you can change the code and run it again. Nothing to install.
- It's the language of the Daily Challenge games, which have their own course.
- The ideas are the same in Python, C or Java. What changes is how you write it, not how you think about it.
The code has already run: next to it (on the right, or below on a phone) is what console.log printed. Change "aguacate" to something else and run it again.
// A shopping list: an array (in order)
const lista = ["tortillas", "frijol", "aguacate"];
console.log(lista);
console.log(lista.length);
// A product card: an object (by name)
const producto = { nombre: "aguacate", precio: 45 };
console.log(producto.precio);
- The array prints with its brackets and its items in the order you wrote them.
lengthcounts how many there are: 3.- You ask the object for a value by name (
precio), not by position.
Same data, different cost
Here are the same three products stored two ways. Both answer "is there any frijol?", but differently:
- The array checks one by one until it finds it. With three you don't notice; with a million, you do.
- The set (
Set) goes straight there, as if it knew which drawer it's in.
In exchange, the set doesn't keep a useful order and doesn't allow duplicates. No structure beats all the others: one is better for what you're going to do.
const enArreglo = ["tortillas", "frijol", "aguacate"];
const enConjunto = new Set(enArreglo);
console.log(enArreglo.includes("frijol")); // checks one by one
console.log(enConjunto.has("frijol")); // goes straight there
enConjunto.add("frijol"); // already there: no duplicate
console.log(enConjunto.size);
The first two print true: same answer, different amount of work to get there. And the set's size stays at 3 even though you added another frijol.