Arrays & Hashmaps
Master the fundamental building blocks of data structures
Progress
0 / 50 completedCore Concepts
Arrays
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together.
- O(1) Access time via index
- Fixed size (static arrays) or Dynamic size
- Contiguous memory allocation
- Cache friendly due to locality
Hashmaps
A Hashmap (or Hash Table) is a data structure that implements an associative array abstract data type, a structure that can map keys to values.
- O(1) Average time for Insert/Delete/Search
- Uses a Hash Function to compute index
- Handles collisions via Chaining or Open Addressing
- Unordered storage of elements
Implementation Details
std::vector (Dynamic Array)
// Initialization
vector<int> arr = {1, 2, 3};
vector<int> zeros(5, 0); // Size 5, all 0s
// Operations
arr.push_back(4); // O(1) amortized
arr.pop_back(); // O(1)
int x = arr[0]; // O(1) access
arr.size(); // O(1)
std::unordered_map (Hashmap)
// Initialization
unordered_map<string, int> map;
// Operations
map["key"] = 10; // O(1) avg, O(n) worst
if (map.find("key") != map.end()) { ... }
map.erase("key"); // O(1) avg
Time Complexity Cheat Sheet
| Operation | Array / Vector | Hashmap / Dictionary |
|---|---|---|
| Access | O(1) | N/A |
| Search | O(n) | O(1) avg |
| Insert (End) | O(1) | O(1) avg |
| Insert (Middle) | O(n) | N/A |
| Delete | O(n) | O(1) avg |