Home »
Data Structure »
Hashing Coding Problems
Minimum deletions to make all elements same using hash table
Here, we are going to see how efficiently we can use a hash table to find the minimum number of deletions required to make all elements of the array same?
Submitted by Radib Kar, on July 02, 2020
Prerequisite: Hashing data structure
Problem statement:
Find minimum number of deletions to make all elements same.
Example:
Input array = [12, 13 ,4, 12, 12, 15, 12, 17]
The minimum number of deletion required is 4. After deleting 4 elements we will get array [12, 12, 12, 12]. You can try the combinations but it's optimum.
Solution:
We can solve this problem using a hash map (hash table). One thing is clear that to achieve minimum deletion we need to keep the elements that have the highest number of occurrences and need to delete the rest of the array elements. So, our target is to find the array element with the highest frequency. After finding the array element we will delete the other elements and that will be our answer.
So how can we design the problem with the hash table and what will be the hash function?
Okay, so here each element is our key and the hash table is has the size of the range of the array. So, if the range of the array is [0,15] then the hash table size would be 15. What will be our hash function and how would we map the keys to the corresponding location in the hash table?
The hash function h(x)=x here but instead of storing the key itself using linear probing we will keep the count only as it does not make any sense to use linear probing as each unique key will have a unique location.
So, for example, if we have three 2s as our key, the hash table will store the count (frequency) at location 2.
So, after the hash table is created we can easily find the most occurred elements by each location (index) of the hash table.
So the algorithm will be:
Step 1:
Create the hash table like below,
Initially hash table is empty
For each key in input array,
Hash[key]++
Step 2:
Initially max_freq=0
For each location
If(hash[location]>max_freq)
Update max_freq as hash[location]
After this max_freq contains the number of occurrences
for the most frequent key and the rest of
the keys need to be deleted.
So the minimum number of deletion:
n-max_freq
where, n = total number of keys/input elements
Dry run with the example:
Input array is = [12, 13 ,4, 12, 12, 15, 12, 17]
So hash table size would be (17-4)=13
After creating the hash table as step1 we will have,
Index Value
4 1
12 4
13 1
15 1
17 1
So max_freq = 4
Hence,
minimum deletion needed = 8-4
C++ implementation:
// C++ program to find minimum deletion
// to make all elements same
#include <bits/stdc++.h>
using namespace std;
int minimum_no_of_deletion(vector<int> arr, int n)
{
//create a hash table where for each key
//the hash function is h(x)=x
//we will use stl map as hash table and
//will keep frequencies stored
//so say two keys are mapping to the same location,
//then the location will have value 2
//instead of the keys itself
map<int, int> hash;
for (auto i : arr) { //for each number
hash[i]++;
}
//now to make all elements same
//we need to keep only the keys with
//maximum frequency
//we need to delete the other keys
//so find the key with max frequency
int maxfreq = 0;
for (auto it = hash.begin(); it != hash.end(); it++) {
if (it->second > maxfreq) {
maxfreq = it->second;
}
}
//so we need to dlete rest of
//the elements n-maxfreq
return n - maxfreq;
}
int main()
{
int n;
cout << "Enter number of elements\n";
cin >> n;
vector<int> arr(n, 0);
cout << "Input the array elements\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Minimum number of deletion required to make all elements same is: ";
cout << minimum_no_of_deletion(arr, n) << endl;
return 0;
}
Output:
Enter number of elements
8
Input the array elements
12 13 14 12 12 15 12 17
Minimum number of deletion required to make all elements same is: 4