<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>HashTable on mizu-blog</title><link>https://suigetsu312.github.io/blog/tags/hashtable/</link><description>Recent content in HashTable on mizu-blog</description><generator>Hugo -- 0.157.0</generator><language>zh-tw</language><lastBuildDate>Mon, 02 Mar 2026 00:35:15 +0800</lastBuildDate><atom:link href="https://suigetsu312.github.io/blog/tags/hashtable/index.xml" rel="self" type="application/rss+xml"/><item><title>TopKFrequentElements</title><link>https://suigetsu312.github.io/blog/posts/topkfrequentelements/</link><pubDate>Mon, 02 Mar 2026 00:35:15 +0800</pubDate><guid>https://suigetsu312.github.io/blog/posts/topkfrequentelements/</guid><description>&lt;h1 id="topkfrequentelements"&gt;TopKFrequentElements&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;br&gt;
&lt;strong&gt;Topics:&lt;/strong&gt; Hash Table, Heap, Frequency Counting&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="approach"&gt;Approach&lt;/h2&gt;
&lt;p&gt;This problem asks us to find the k most frequent elements.
We first use an &lt;code&gt;unordered_map&lt;/code&gt; to count the frequency of each element.
Then we use a &lt;code&gt;min-heap&lt;/code&gt; of size k to maintain the current top-k elements.
When the heap size exceeds k, we remove the smallest element.
Finally, we obtain a list of size k that contains the top-k elements.&lt;/p&gt;</description></item></channel></rss>