Skip to content

Search and sort

Techniques for finding things fast: swapping a nested loop for a map, sorting first or halving the search space.

3 guides35 min in totalAlgorithms

The guides, in order

Read them top to bottom: each one builds on the ones before. The ones you've read carry a check.

About this group

What you'll find
O(1) lookups with maps and sets, sorting to solve in one pass (neighbours, intervals, comparators) and binary search, including binary search on the answer.
It's for you if…
your first solution usually has two nested loops and you don't know how to make it faster.
How each guide works
Each guide starts with the intuition and the signs that tell you to use the technique, then the steps and its time and memory cost. It includes a step-by-step traced example, code in TypeScript, Python and PHP, variants, common mistakes and frequently asked questions.