AlgoViz

Sets and maps

easy 16 min

A set stores unique keys and a map stores one value per key.

Where it shows up

The same pattern appears across everyday software.

  • Python itself

    Every module namespace, object attribute table and keyword-argument set is a dict, and JSON objects load as dicts. Hash-backed maps are the most used data structure in the language.

  • Database indexes

    An index on a column is an ordered map from value to row location, stored as a B-tree. Ordering is what lets it answer WHERE price BETWEEN 10 AND 20 or ORDER BY without scanning the whole table.

  • Exchange order books

    Buy and sell orders are grouped by price level in sorted maps, so the best bid (the largest key) and the best ask (the smallest key) are always one lookup away and new orders slot into place in O(log n).

  • The Linux CPU scheduler

    The Completely Fair Scheduler keeps runnable tasks in a red-black tree keyed by how much CPU time each has used, and always runs the leftmost task: an ordered set in the kernel.

  • Caches, dedupe and blocklists

    Web caches map URLs to responses, crawlers keep a set of visited pages, and spam filters check senders against a blocklist set. All need fast membership, not order, so they are hash-backed.