Saturday, April 27, 2024
HomeProgrammingEmbarking on a 100-day adventure: Day 5 - No.Of Occurrences In An...

Embarking on a 100-day adventure: Day 5 – No.Of Occurrences In An Array

-

Welcome to Day 5 of our coding journey! Today, we dive deep into the world of programming to tackle a common, yet curious problem: finding the number of occurrences of a specific value within an array. Brace yourself as we unravel the magic behind an elegant solution utilizing the power of HashMaps.

The Journey for Occurrence Count:

In the middle of our array adventures, a common challenge surfaces—how often does a particular value appear within an array? This simple question unfolds into a journey that starts with developers across various domains. In data analysis, understanding the distribution of values becomes paramount. In algorithmic problem-solving, decoding the frequency of the key elements is often the key to optimization.

Introducing the HashMap:

In our search for an efficient solution, we turn to the flexible HashMap, a key-value pair storage mechanism within the extensive Collections framework. The HashMap proves to be our trusted partner in effortlessly tallying the occurrences of each element in an array.

The Code Unveiled:

Let’s take a peek at the code that makes this magic happen:

public class NoOfOccuranceInAnArray {
public static void main(String[] args) {
//Given an integer array a and value v,
//find the number of Occurancences of v in a
int [] a = {1,2,2,2,2,3,4,5,5,6,7,7,};
int v = 2;
// Create a HashMap to store element-frequency pairs
Map<Integer,Integer> map = new HashMap<>();
// Iterate through the array to populate the HashMap
for(int i : a){
if(map.containsKey(i)){
// If the element is already in the map, increment its frequency
map.put(i,map.get(i) + 1);
}
else{
// If the element is not in the map, add it with a frequency of 1
map.put(i,1);
}
}
System.out.println(map.get(v));
}
}

Breaking Down The Code:

The code begins by initializing an integer array ‘a’ containing a sequence of numbers. The variable ‘v’ represents the value whose occurrences we want to count within the array. A ‘HashMap’ named ‘map’ is created to store element-frequency pairs. The key represents the array element, and the value represents the frequency (number of occurrences). The code uses a ‘for each’ loop to iterate through each element of the array ‘a’. For each element ‘i’, the code checks whether it is already in the ‘HashMap ‘ using ‘map.containsKey(i)’.

If present, it increments the frequency by 1. If not present, it adds the element to the ‘ HashMap ‘ with a frequency of 1. Finally, the code prints the number of occurrences of the target value ‘ v ‘ by retrieving the frequency from the ‘ HashMap ‘ using ‘ map.get(v) ‘.

Time Complexity:

The time complexity is O(N) because we are iterating through each element from the given array.

Space Complexity:

Space complexity is how much memory we need. We use a HashMap to remember numbers and their counts. If there are 10 different numbers, we use memory for 10 things; if there are 100, we use memory for 100 things. So, space complexity is O(N), where N is the number of unique elements in the array.

Wrapping Up:

In this blog post, we’ve explored a simple and effective way to find the number of occurrences of a specific value in an array using Java. The use of a HashMap allows us to efficiently track the frequency of each element, providing a clear and concise solution to the problem. Happy Coding!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

3 Sum | Leetcode #15 | 100 Days of Code Day 10

Today we are going to solve 3 Sum - our first Medium Level Array problem from Coderefer DSA Sheet. If you are new to this...

Best Time to Buy and Sell Stock – Day 7 | 100 Days of Code

Welcome to Day 7 of 100 Days of Code where today we solve yet another most frequently asked easy array problem called - Best Time...

Contains Duplicate – Day 8 | 100 Days of Code

Welcome to Day 8 of 100 Days of Code and today we are going to solve yet another easy-level problem - Contains Duplicate as part...

Two Sum – Day 6 | 100 Days of Code

Welcome to Day 6 of 100 Days of Code where we solve the most frequently asked Easy level Array problem - Two Sum. Let us...

Follow us

1,358FansLike
10FollowersFollow
400SubscribersSubscribe

Most Popular