哈希表中能有相同元素吗_最小删除以使用哈希表使所有元素相同

哈希表中能有相同元素吗

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.

输入数组= [12、13、4、12、12、15、12、17]
删除的最小数量为4。删除4个元素后,我们将得到数组[12,12,12,12]。 您可以尝试组合,但这是最佳选择。

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?

好的,所以这里的每个元素都是我们的键,哈希表具有数组范围的大小。 因此,如果数组的范围为[0,15],则哈希表大小将为15 。 我们的哈希函数将是什么?如何将键映射到哈希表中的对应位置?

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.

此处的哈希函数h(x)= x ,而不是使用线性探测来存储密钥本身,我们将仅保留计数,因为使用线性探测没有意义,因为每个唯一的密钥都将具有唯一的位置。

So, for example, if we have three 2s as our key, the hash table will store the count (frequency) at location 2.

因此,例如,如果我们有三个2作为密钥,则哈希表将在位置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:

第1步:

Create the hash table like below,
Initially hash table is empty

For each key in input array,
Hash[key]++

Step 2:

第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 ++实现:

// 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


翻译自: https://www.includehelp.com/data-structure-tutorial/minimum-deletions-to-make-all-elements-same-using-hash-table.aspx

哈希表中能有相同元素吗

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值