DSA Patterns: Number 1 Using HashMap

Table of contents

No heading

No headings in the article.

Data Structures and Algorithms are an essential part of coding interviews in many companies most Big Tech companies(Example:-Google, Microsoft, Apple, Amazon, etc….) types ask it for the selection of the ideal SDE and it is not only limited to entry-level but also for SDE-2 and higher level

The best way to master Data Structure and Algorithms is to practice, practice, and a lot of practice!!!!

I am currently doing the (though I procrastinate sometimes) and I mostly use leetcode for it as it is recommended by everyone while practicing I found out few patterns in the question, the trick to solve them was simple, use HashMap(Java) or map(C++), and similar DS for other languages

It is that in this Data Structure, it stores elements against the frequency(Number of times that element has appeared) thus we can easily do the required operation which is asked in the question

To name a few questions:-

Majority elements 1 (Link:-https://leetcode.com/problems/majority-element/)

Majority elements 2 (Link:-https://leetcode.com/problems/majority-element-ii/)

Most frequent element (Link:-https://www.geeksforgeeks.org/frequent-element-array/)
// Top K frequent element(Not finalized yet)

and many more……

SOLUTIONS:-

Majority elements 1

Majority Elements 2:

You will notice that these questions mostly deal with the frequency of the element(the number of times the element occur) like finding which element occurs N times or which element is the most occurring element

Though you can solve these questions in Order of N^2 Complexity i.e O(N^2) using the Brute Force method (The method which comes into our mind after seeing the question for the first time) of using 2 for loops (Nested Loops). But to make it more optimized you can use HashMap and reduce the complexity to Order of N i.e O(N) or Linear Time Complexity.