What a data structure is (and why you already use them)
A data structure is a way of arranging data so that some operation is fast. An array is fast to index and slow to search; a Map is fast to search and has no positions. An algorithm is a recipe that uses those operations. You have used both since Module 03 — this module makes the trade-offs explicit, because choosing the structure is most of solving the problem.
You should see
array search: much slower than the Set
set search: instant"user199999" to "user0" (the first item). Why is the array suddenly fast too? That is why we talk about the worst case.| Structure | JavaScript | Index by position | Search by value | Add / remove at end | Add / remove at front | Ordered? |
|---|---|---|---|---|---|---|
| Dynamic array | Array | O(1) | O(n) | O(1) | O(n) | yes, by insertion |
| Hash map | Map (or object) | — | O(1) by key | O(1) | — | insertion order kept |
| Hash set | Set | — | O(1) | O(1) | — | insertion order kept |
| Stack | Array (push/pop) | — | O(n) | O(1) | — | LIFO |
| Queue | build it (no built-in!) | O(n) | O(n) | O(1) | O(1) | FIFO |
| Linked list | build it | O(n) | O(n) | O(1) with tail | O(1) | yes |
| Binary search tree | build it | — | O(log n) if balanced | O(log n) | — | sorted |
| Heap | build it (no built-in!) | — | O(n) | O(log n) | min in O(1) | partial |
| Graph | Map of arrays | — | BFS / DFS O(V+E) | O(1) | — | no |
