{"id":727,"date":"2026-11-21T07:58:21","date_gmt":"2026-11-20T23:58:21","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/21\/hash-table-look-up-a-key-in-the-slot-its-hash-names\/"},"modified":"2026-11-21T07:58:21","modified_gmt":"2026-11-20T23:58:21","slug":"hash-table-look-up-a-key-in-the-slot-its-hash-names","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/21\/hash-table-look-up-a-key-in-the-slot-its-hash-names\/","title":{"rendered":"Hash table \u2014 look up a key in the slot its hash names"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>A hash table<\/strong> finds a key by turning it into a slot number and reading that slot. It fits a collection that will be asked many times, when the only question is whether the key is present. It does not fit a single query, a request for the next larger key, or a walk in stored order.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What problem it solves<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A linear scan reads every item until the key matches or the sequence ends. Ask the same collection again and the scan repeats. A hash table pays for one pass that stores each key, then answers later questions from the slot the key names.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Take <code>[4, 2, 7, 3, 9]<\/code> and five slots, numbered 0 to 4. The hash here is the remainder modulo 5: the slot is <code>value % 5<\/code>.<\/p>\n\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th style=\"text-align: right;\">value<\/th>\n<th style=\"text-align: right;\">remainder<\/th>\n<th style=\"text-align: right;\">slot<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: right;\">4<\/td>\n<td style=\"text-align: right;\">4<\/td>\n<td style=\"text-align: right;\">4<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\">2<\/td>\n<td style=\"text-align: right;\">2<\/td>\n<td style=\"text-align: right;\">2<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\">7<\/td>\n<td style=\"text-align: right;\">2<\/td>\n<td style=\"text-align: right;\">2<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\">3<\/td>\n<td style=\"text-align: right;\">3<\/td>\n<td style=\"text-align: right;\">3<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\">9<\/td>\n<td style=\"text-align: right;\">4<\/td>\n<td style=\"text-align: right;\">4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\"><code>7<\/code> and <code>2<\/code> name the same slot. So do <code>9<\/code> and <code>4<\/code>. A slot that already holds a key keeps a short chain. The table after that one pass is:<\/p>\n\n<pre><code>0:  (empty)\n1:  (empty)\n2:  2 \u2192 7\n3:  3\n4:  4 \u2192 9\n<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Building it read each of the five keys once. That preparation is <strong>\u0398(n)<\/strong>.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A later lookup hashes the key and reads only that chain. <code>3<\/code> names slot 3, and the first key there is <code>3<\/code>. <code>7<\/code> names slot 2, which starts with <code>2<\/code> and then holds <code>7<\/code>. <code>8<\/code> also names slot 3, the chain ends at <code>3<\/code>, and <code>8<\/code> is absent. <code>1<\/code> names slot 1, which is empty. None of these reads the other slots.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The chains are why the lookup <strong>expects<\/strong> constant time rather than promising it. A usable hash spreads keys, and the table grows when it gets crowded, so a chain stays a few keys long no matter how large n is. If every key landed in slot 2, that chain would be the whole set and the lookup would be a scan again. <strong>Expected \u0398(1)<\/strong> means that pile-up is rare. The worst case is still <strong>\u0398(n)<\/strong>.<\/p>\n\n\n<p class=\"wp-block-paragraph\">This remainder is a teaching hash. A hash of a string mixes the characters and then takes a remainder. The two steps are the same: name a slot, then look only there. A cryptographic hash such as SHA-256 is a different job. It fingerprints a message. It does not choose a slot in a table.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A hash table is the usual structure behind a dictionary of keys.<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>Python&#8217;s <code>dict<\/code> and <code>set<\/code>, Java&#8217;s <code>HashMap<\/code>, and JavaScript&#8217;s <code>Map<\/code> store a key by hashing it and keep the record in that slot.<\/li>\n<li>A database hash index answers <code>WHERE id = 42<\/code> the same way. A range such as <code>id &gt; 42<\/code> wants an ordered index instead.<\/li>\n<li>A cache keyed by a URL, and a compiler&#8217;s table of variable names, are hash tables. Repeated equality tests are the whole workload.<\/li>\n<li>The scan in <a href=\"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/linear-search-find-a-key-by-scanning-each-item-once\/\">linear search<\/a> is what you do before this table exists, or when the collection is short enough that the pass to build it costs more than the questions you will ask.<\/li>\n<\/ul>\n\n\n<p class=\"wp-block-paragraph\">Drop it when you will ask once. The build is already a full pass, and one scan would have answered. Drop it when the question is the next key, a range, or a prefix. Those need an ordered tree or a trie. Keep it when the same set of keys will be tested for equality many times.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Working code<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><code>HashTable<\/code> uses <code>key % 5<\/code> and a chain in each slot. After inserting <code>4, 2, 7, 3, 9<\/code>, the slots are <code>[[], [], [2, 7], [3], [4, 9]]<\/code>. Membership then follows one chain: <code>3<\/code> and <code>7<\/code> are present, <code>8<\/code> and <code>1<\/code> are not.<\/p>\n\n<pre><code class=\"language-python\">class HashTable:\n    def __init__(self, n_slots):\n        self.slots = [[] for _ in range(n_slots)]\n\n    def add(self, key):\n        chain = self.slots[key % len(self.slots)]\n        if key not in chain:\n            chain.append(key)\n\n    def __contains__(self, key):\n        return key in self.slots[key % len(self.slots)]\n\n\ntable = HashTable(5)\nfor value in (4, 2, 7, 3, 9):\n    table.add(value)\n\nassert table.slots == [[], [], [2, 7], [3], [4, 9]]\nassert 3 in table\nassert 7 in table\nassert 8 not in table\nassert 1 not in table\nprint(table.slots)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 21 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A hash table finds a key by turning it into a slot number and reading that slot. It fits a collection that will be asked many times, when the only question is whether the key is present. It does not fit a single query, a request for the next&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,43],"tags":[],"class_list":["post-727","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-searching"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=727"}],"version-history":[{"count":0,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/727\/revisions"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}