All lessonsOpen Open Open Open Open
2. Arrays & Hashing
Hash tables
0 of 5 activities0%
Reading 1
Key → bucket
A hash function turns a key into an index. Collisions happen; chaining or open addressing resolves them.
In C++, unordered_map and unordered_set give hash-based dictionaries and sets. Average insert/lookup/delete is O(1); worst case can degrade.
Use hashing when you need fast membership or counting.
#include <unordered_map>
unordered_map<int,int> freq;
freq[7]++;
if (freq.count(7)) cout << freq[7];Check 2
Average lookup
Average-case lookup in a hash table is
Fill in 3
STL map
Try it 4
Count with map
Frequency of values.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
First duplicate
Read n then n ints. Print the first value that appears twice (left to right). If none, print -1.
main.cpp
Loading editor…