## Approach

The idea is to store each element's occurrence in a hash and then count the occurrence of each frequency in a hash again. Then we will sort the frequency of occurrences of each element in increasing order and will start removing the element whose frequency is less or equal to m.

### Algorithm

- Create a hashmap to store the occurrence of elements.
- Count the occurrence of all elements and store them in the hash.
- Sort the hash in increasing order.
- Remove the elements whose frequency is less than m from the hashmap.
- Print the number of elements present in the hash.

### Implementation in C++

```
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#define pb push_back
int hash_fun(int arr[], int n, int m)
{
int temp = 0;
// hashmap for storing the occurence
unordered_map<int, int> mp;
vector<pair<int, int>> v;
for (int i = 0; i < n; i++)
mp[arr[i]]++;
// use vector to store the pair key with its occurrence
for (auto i = mp.begin(); i != mp.end(); i++)
v.pb(make_pair(i->second, i->first));
sort(v.begin(), v.end()); // sorting the vector
int vec_size = v.size();
for (int i = 0; i < vec_size; i++)
{
// Remove the value if it is less than or equal to m
// else return the remaining size
if (v[i].first <= m)
{
m = m - v[i].first;
temp++;
}
else
return vec_size - temp;
}
return vec_size - temp;
}
int main()
{
int m;
int arr[] = {3, 1, 2, 2, 3, 3};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Enter the value of m : ";
cin >> m;
cout << endl;
cout << hash_fun(arr, size, m);
return 0;
}
```

**Input**

`Enter the value of m : 3`

**Output**

`1`

#### Time Complexity

The time complexity of the above approach is O(NlogN) because we have used sorting, which marks the upper bound for time complexity.

#### Space Complexity

The space complexity of the approach is O(N), as we are storing the elements as a key in the hashmap.

You can also read about the __Longest Consecutive Sequence__.

## Frequently Asked Questions

**What is HashMap data structure?**

A HashMap is a data structure that can map certain keys to a specific value. The values and keys could be anything.

**Is sort in C++ STL?**

A C++ Standard Template Library(STL) includes a built-in function called sort(). The elements in the range can be sorted using this function in either ascending or descending order.

**What is the time complexity of sort function in C++ STL?**

The worst-case time complexity of sort() is in O(n log n), where n is the number of sorted elements.

**Also Read - **Strong number in c

## Conclusion

In this article, we have extensively discussed the problem of printing the minimum number of distinct elements after removing m items. We solved the problem using the hashmap data structure and discussed its time as well as space complexity.

We hope that this blog has helped you enhance your knowledge about the count of minimum distinct elements and if you like to learn more, check out our articles __Implementation of HashMap__, __Sorting Based Problems__, __STL in C++__, and many more on our Website.

**Recommended Problems - **

Refer to our __Guided Path__ on __Coding Ninjas Studio__ to upskill yourself in __Data Structures and Algorithms__, __Competitive Programming__, __JavaScript__, __System Design__, and many more! If you want to test your competency in coding, you may check out the mock __test series__ and participate in the __contests__ hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the __problems__, __interview experiences,__ and __interview bundle__ for placement preparations.

Do upvote our blogs if you find them helpful and engaging!

**Happy Learning!**