Technology3 min read

Finding the First Element Occurring K Times

Abhishek Ahlawat

By Abhishek Ahlawat โ€” Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#hashmap#hashing#cpp

Summarize this article using AI

Let's start by understanding the problem statement of this task.

You are given an array, your work is to find the element which occurs exactly k times in a given array.

Hint:

For printing the number in an array which occurs k times can be done by storing the frequency of each integer and print the integer whose frequency occurs exactly k times in the array in less time complexity.

Quick Think:

For solving the above algorithm, we need to ponder over the following points:

  • Store the frequency of each element in the array in an unordered map.
  • If the frequency matches the count of that particular integer, then print out the element.

Algorithm:

Create a function to find the frequency of the elements in the array with arguments as the array the size of the array and the count of the number of times the element occurs and follow the following steps:-

Step1: Traverse the array and keep pushing the frequency of each element in the map.

Step2: Now, check if the count of the given element in the map is equal to the frequency given, then print out the element else continue.

Step3: If the element is not found in the array, then return -1.

Implementation Of The Above Algorithm:

Now let's see the implementation of the above algorithm,

#include <bits/stdc++.h>
using namespace std;

/*function to find the first element
occurring k number of times. */
int firstElement(int arr[], int n, int k)
{
	unordered_map<int, int> count_map;
	for (int i = 0; i < n; i++)
		count_map[arr[i]]++;

	for (int i = 0; i < n; i++)
		/*if count of element == k ,then
		it is the required first element. */
		if (count_map[arr[i]] == k)
			return arr[i];
	return -1;
}

// Driver program to test above algorithm. */
int main()
{
	int arr[] = { 1, 7, 4, 3, 4, 8, 7 };

	int n = sizeof(arr) / sizeof(arr[0]);
	int k = 2;
	cout << firstElement(arr, n, k);
	return 0;
}

The time complexity of the above algorithm is O(n).

The output of the above algorithm is 7.

Explanation Of The Above Algorithm:

Let us consider an array as shown in the above diagram and follow the step to understand the algorithm.

Finding the First Element Occurring Exactly K Times in a Given Array Using Unordered Map in C++

  • The frequency of all the elements in the array is pushed as shown in the diagram above.
  • Now, traversing the array we find the first element as 1, now since the frequency of the element is 1, hence we move to the next array element.

Finding the First Element Occurring Exactly K Times in a Given Array Using Unordered Map in C++

  • Now, the next array element is 7 since the frequency of the element is 2 so, we output the result as shown in the diagram above.

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next