<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Parikshitabhoge on Medium]]></title>
        <description><![CDATA[Stories by Parikshitabhoge on Medium]]></description>
        <link>https://medium.com/@parikshitabhoge?source=rss-c2b6dcb7d50d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*9HmtG55fCMYSassN</url>
            <title>Stories by Parikshitabhoge on Medium</title>
            <link>https://medium.com/@parikshitabhoge?source=rss-c2b6dcb7d50d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 15:28:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@parikshitabhoge/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[I was solving LeetCode blindly — until HashMaps changed everything!]]></title>
            <link>https://medium.com/@parikshitabhoge/i-was-solving-leetcode-blindly-until-hashmaps-changed-everything-85e60d5e8ba4?source=rss-c2b6dcb7d50d------2</link>
            <guid isPermaLink="false">https://medium.com/p/85e60d5e8ba4</guid>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[dsa-problem]]></category>
            <category><![CDATA[hashset]]></category>
            <category><![CDATA[hashmap]]></category>
            <dc:creator><![CDATA[Parikshitabhoge]]></dc:creator>
            <pubDate>Thu, 13 Aug 2026 11:59:04 GMT</pubDate>
            <atom:updated>2026-08-13T11:59:04.515Z</atom:updated>
            <content:encoded><![CDATA[<p>When I first started solving LeetCode problems, I had a habit that I didn’t realize was a problem.</p><p>I would read the question, understand what it was asking, and immediately start thinking about <strong>how to make the code work</strong>.</p><p>Not about the pattern.</p><p>Not about the data structure.</p><p>Just:</p><blockquote><em>“Okay… how do I solve this?”</em></blockquote><p>And that usually led me to the most obvious solution.</p><p>Then came the <strong>TLE</strong>.</p><p>Time Limit Exceeded.</p><p>So I would try to optimize a little, change a loop here, change something there, and hope it would pass.</p><p>Sometimes it did.</p><p>Sometimes it didn’t.</p><p>And honestly, the frustrating part wasn’t just getting TLE. It was not understanding <strong>why my solution was slow in the first place</strong>.</p><p>A lot of my solutions were basically:</p><pre>for every element<br>    check something by going through the array again</pre><p>Which meant I was repeatedly searching through the same data.</p><p>In other words, I was solving problems, but I wasn’t really recognizing the <strong>patterns behind them</strong>.</p><p>That started changing when I learned about <strong>HashMaps and HashSets</strong>.</p><p>And two very simple LeetCode problems made that change click for me:</p><ul><li>Majority Element</li><li>Contains Duplicate</li></ul><p>They weren’t the hardest problems I’ve solved.</p><p>But they changed the way I look at problems.</p><h3>So, what did HashMaps actually change for me?</h3><p>Before learning them properly, I mostly thought of a map or set as just another C++ data structure I had to remember.</p><p>Now I think about them differently.</p><p>A <strong>HashMap</strong> is basically my way of saying:</p><blockquote><em>“I want to remember something about each value so I don’t have to keep searching for it again.”</em></blockquote><p>It stores things as <strong>key → value</strong>.</p><p>For example:</p><pre>apple → 3<br>banana → 5<br>orange → 2</pre><p>Here, the key is the thing I’m interested in, and the value can store some information about it — like its frequency.</p><p>A <strong>HashSet</strong> is even simpler:</p><blockquote><em>“I only care whether I’ve seen this value before.”</em></blockquote><p>I don’t need to store a count.</p><p>I just need:</p><pre>Have I seen this?<br>YES / NO</pre><p>And that’s when something clicked for me:</p><p><strong>Instead of repeatedly searching through an array, I can remember what I’ve already seen.</strong></p><p>That small change can completely change the time complexity.</p><h3>Problem 1: Majority Element</h3><p>The problem is simple:</p><p>Given an array, find the element that appears <strong>more than n/2 times</strong>.</p><p>For example:</p><pre>nums = [2, 2, 1, 1, 1, 2, 2]</pre><p>The answer is:</p><pre>2</pre><p>because 2 appears more than half of the time.</p><h3>My first instinct: brute force</h3><p>The obvious way to think about this problem is:</p><blockquote><em>“For every element, count how many times it appears.”</em></blockquote><p>So for every number, I could loop through the entire array and count its occurrences.</p><p>Something like:</p><pre>Take nums[i]<br>    ↓<br>Search the whole array<br>    ↓<br>Count how many times nums[i] occurs<br>    ↓<br>Compare with the highest count</pre><p>And then do this again for the next element.</p><p>The problem?</p><p>If there are n elements and I&#39;m scanning n elements for each one, I&#39;m doing roughly:</p><pre>n × n = O(n²)</pre><p>operations.</p><p>That was the kind of solution that kept getting me closer to TLE.</p><p>The code may be correct.</p><p>But correctness isn’t the only thing that matters in DSA.</p><p><strong>How efficiently are you getting the answer?</strong></p><h3>Then I thought: “Why am I counting the same thing again and again?”</h3><p>This was the pattern I started noticing in my notes.</p><p>Whenever I see:</p><blockquote><em>“How many times does this occur?”</em></blockquote><p>I should immediately think:</p><p><strong>frequency → HashMap</strong></p><p>Instead of repeatedly counting the same values, I can count everything in <strong>one pass</strong>.</p><p>For every number:</p><pre>m[nums[i]]++;</pre><p>So if:</p><pre>[2, 2, 1, 1, 1, 2, 2]</pre><p>my map eventually becomes something like:</p><pre>2 → 4<br>1 → 3</pre><p>Now I already know the frequency of every number.</p><p>No repeated searching.</p><p>No nested loop.</p><p>Just remember the count while going through the array.</p><h3>My HashMap solution</h3><pre>class Solution {<br>public:<br>    int majorityElement(vector&lt;int&gt;&amp; nums) {<br>        unordered_map&lt;int, int&gt; m;</pre><pre>        // Step 1: count occurrences of each number<br>        for (int i = 0; i &lt; nums.size(); i++) {<br>            m[nums[i]]++;<br>        }</pre><pre>        // Step 2: find the key with the highest count<br>        int maxCount = 0;<br>        int answer = -1;</pre><pre>        for (auto p : m) {<br>            if (p.second &gt; maxCount) {<br>                answer = p.first;<br>                maxCount = p.second;<br>            }<br>        }</pre><pre>        return answer;<br>    }<br>};</pre><h3>How my solution works</h3><p>I broke the problem into two simple steps.</p><h3>Step 1: Count everything</h3><pre>for (int i = 0; i &lt; nums.size(); i++) {<br>    m[nums[i]]++;<br>}</pre><p>Every number becomes a key.</p><p>Its count becomes the value.</p><p>For:</p><pre>[2, 2, 1, 1, 1, 2, 2]</pre><p>we get:</p><pre>2 → 4<br>1 → 3</pre><h3>Step 2: Find the highest count</h3><p>Now I go through the map:</p><pre>for (auto p : m) {<br>    if (p.second &gt; maxCount) {<br>        answer = p.first;<br>        maxCount = p.second;<br>    }<br>}</pre><p>Here:</p><ul><li>p.first = the number</li><li>p.second = how many times it appeared</li></ul><p>So I simply keep track of the key with the highest frequency.</p><p>One thing that surprised me here was that I didn’t need the map to be sorted.</p><p>unordered_map doesn&#39;t care about the order of the keys.</p><p>I only care about their <strong>counts</strong>.</p><p>So instead of trying to arrange everything, I can simply ask:</p><blockquote><em>“Which key has the highest frequency?”</em></blockquote><p>That was a small but useful shift in how I think.</p><h3>Complexity</h3><p>Average time:</p><p><strong>O(n)</strong></p><p>Space:</p><p><strong>O(n)</strong></p><p>I traded some extra memory for a much faster lookup/counting process.</p><p>And that idea became even clearer in the next problem.</p><h3>Problem 2: Contains Duplicate</h3><p>This one asks whether an array contains any duplicate value.</p><p>For example:</p><pre>[1, 2, 3, 1]</pre><p>There is a duplicate 1.</p><p>So the answer is:</p><pre>true</pre><p>But:</p><pre>[1, 2, 3, 4]</pre><p>contains no duplicate.</p><p>So:</p><pre>false</pre><h3>The brute-force way</h3><p>Again, my first thought could be:</p><blockquote><em>“Take one element and compare it with every other element.”</em></blockquote><p>Then take the next element and do it again.</p><p>For example:</p><pre>1 → compare with everything<br>2 → compare with everything<br>3 → compare with everything<br>...</pre><p>This can again lead to:</p><p><strong>O(n²)</strong> time.</p><p>And I started noticing something.</p><p>I’m not really interested in <em>where</em> the duplicate is.</p><p>I’m interested in one simple question:</p><blockquote><strong><em>“Have I seen this number before?”</em></strong></blockquote><p>That’s a HashSet problem.</p><h3>HashSet changed the way I looked at it</h3><p>Instead of repeatedly comparing values, I can keep a set called:</p><pre>seen</pre><p>Every time I see a number, I ask:</p><pre>Have I already seen this?</pre><p>If yes:</p><p><strong>Duplicate found → return true</strong></p><p>If no:</p><p><strong>Remember it → continue</strong></p><p>That’s exactly what my solution does.</p><pre>class Solution {<br>public:<br>    bool containsDuplicate(vector&lt;int&gt;&amp; nums) {<br>        unordered_set&lt;int&gt; seen;</pre><pre>        for (int i = 0; i &lt; nums.size(); i++) {<br>            if (seen.count(nums[i])) {<br>                return true;<br>            }</pre><pre>            seen.insert(nums[i]);<br>        }</pre><pre>        return false;<br>    }<br>};</pre><h3>How it works</h3><p>Suppose:</p><pre>nums = [1, 2, 3, 1]</pre><p>Initially:</p><pre>seen = {}</pre><p>We see 1.</p><p>Has 1 been seen?</p><p>No.</p><p>So:</p><pre>seen = {1}</pre><p>Then 2:</p><pre>seen = {1, 2}</pre><p>Then 3:</p><pre>seen = {1, 2, 3}</pre><p>Then we see 1 again.</p><p>We ask:</p><pre>seen.count(1)</pre><p>It tells us that 1 is already there.</p><p>So we immediately return:</p><pre>true</pre><p>And this is another thing I liked about this approach:</p><p><strong>I don’t even have to finish scanning the array once I’ve found the answer.</strong></p><p>The moment I find a duplicate, I’m done.</p><h3>Complexity</h3><p>Average time:</p><p><strong>O(n)</strong></p><p>Space:</p><p><strong>O(n)</strong></p><p>Again, I’ve used extra memory to save time.</p><h3>The pattern I finally started seeing</h3><p>After solving these two problems, I went back to my notes.</p><p>And I noticed that I had actually written down some questions I should start asking myself while solving problems:</p><blockquote><em>“Have I seen this before?”</em></blockquote><blockquote><em>“Do elements have something in common?”</em></blockquote><blockquote><em>“How many times does this appear?”</em></blockquote><blockquote><em>“Have I seen this value before?”</em></blockquote><p>These questions are more useful to me now than simply asking:</p><blockquote><em>“Which code should I write?”</em></blockquote><p>Because sometimes the real challenge isn’t writing the code.</p><p>It’s <strong>recognizing what kind of problem you’re looking at.</strong></p><h3>What surprised me about Majority Element</h3><p>The surprising part wasn’t just that HashMap made the problem faster.</p><p>It was that I didn’t have to keep thinking about every element individually.</p><p>My brute-force mindset was:</p><pre>Take one element<br>→ count it<br>→ take another element<br>→ count it again<br>→ repeat...</pre><p>The HashMap mindset became:</p><pre>Go through the array once<br>→ remember what I see<br>→ use that information later</pre><p>That’s a completely different way of thinking.</p><p>I’m no longer asking the array the same question repeatedly.</p><p>I’m <strong>building the information I need while traversing it.</strong></p><p>That idea felt much bigger to me than just learning unordered_map.</p><h3>What surprised me about Contains Duplicate</h3><p>This one made me realize that sometimes I don’t need to store <em>information</em> about a value.</p><p>I only need to know:</p><blockquote><strong><em>“Does it exist?”</em></strong></blockquote><p>That’s where HashSet makes so much sense.</p><p>HashMap:</p><pre>value → information</pre><p>HashSet:</p><pre>value → have I seen it?</pre><p>So now when I see a problem involving:</p><ul><li>duplicates</li><li>checking whether something appeared before</li><li>existence</li><li>unique elements</li></ul><p>my brain is much more likely to consider a <strong>HashSet</strong>.</p><h3>The bigger lesson: Space for Speed</h3><p>After solving both problems, I realized something I had heard before but hadn’t really felt while solving problems:</p><blockquote><strong><em>HashMaps basically trade space for speed.</em></strong></blockquote><p>I’m using extra memory to store information.</p><p>But in return, I can avoid repeatedly scanning the entire array.</p><p>That’s why a problem that naturally leads to an O(n²) solution can often become an average O(n) solution using hashing.</p><p>And for me, this was the real lesson.</p><p>Not:</p><blockquote><em>“Learn HashMap syntax.”</em></blockquote><p>But:</p><blockquote><strong><em>“Use memory to avoid doing the same work again.”</em></strong></blockquote><h3>My new DSA trigger</h3><p>I’m still learning, so I’m definitely not saying HashMap is the answer to everything.</p><p>But I’ve started building a small mental checklist.</p><p>When I see a problem asking about:</p><p><strong>frequency →</strong> think HashMap</p><p><strong>count →</strong> think HashMap</p><p><strong>duplicates →</strong> think HashSet</p><p><strong>have I seen this before? →</strong> think HashSet</p><p><strong>value → information →</strong> think HashMap</p><p>For example:</p><pre>&quot;How many times does each element occur?&quot;<br>        ↓<br>    HashMap</pre><p>and:</p><pre>&quot;Does this value already exist?&quot;<br>        ↓<br>    HashSet</pre><p>It’s simple, but it gives me somewhere to start instead of staring at the problem statement completely blank.</p><h3>I’m Still Learning</h3><p>I still have a long way to go with DSA.</p><p>I’m not at the stage where I can look at every problem and instantly identify the optimal pattern.</p><p>I still get stuck.</p><p>I still write brute-force solutions.</p><p>I still get TLE sometimes.</p><p>But I think that’s okay.</p><p>Because now, when I get stuck, I’m trying to ask a different question:</p><blockquote><strong><em>“What am I repeatedly doing that I could remember instead?”</em></strong></blockquote><p>That question alone has already changed the way I approach problems.</p><p>And I’m slowly realizing that DSA isn’t just about memorizing algorithms.</p><p>It’s about learning to <strong>recognize patterns</strong>.</p><h3>What’s Next?</h3><p>I’ve started moving on to my next DSA pattern:</p><h3>Two Pointers.</h3><p>And honestly, I’m curious to see whether it will change the way I look at arrays and strings the same way HashMaps did.</p><p>For now, one thing is definitely staying with me:</p><blockquote><strong><em>Whenever I see a problem involving frequency, counting, or duplicates, HashMap/HashSet is going to cross my mind.</em></strong></blockquote><p>Not because someone told me to use it.</p><p>But because I’ve finally experienced <strong>why</strong> it works.</p><p>And I think that’s a much better way to learn DSA.</p><p><strong>One pattern at a time.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85e60d5e8ba4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>