哈希表中能有相同元素吗

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 locationIf(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

哈希表中能有相同元素吗

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

  1. 查询员工表中的最大入职时间和最小入职时间的天数差,查询部门编号为90的员工个数

    1.查询员工表中的最大入职时间和最小入职时间的天数差 SELECTDATEDIFF(MAX(hiredate),MIN(hiredate)) AS DIFFERENCE FROMemployees; ...

  2. 循环中 动态参数 传div 层_【转载】黄学杰等:铌元素在锂离子电池中的应用

    来源|储能科学与技术 摘 要: 锂离子电池因其能量密度高.环境污染小等优点得到了广泛应用,但其仍然存在不容忽视的问题,电极材料结构劣化导致的电化学性能下降及热稳定性差等问题仍然比较严重,因此电极材料改 ...

  3. update两个表中的同一字段的数据_用Python实现多个工作簿中的数据按列合并到同一个工作表中...

    1.需求描述 现在有三个工作表,如下: 在这里插入图片描述 并且每个工作表中的内容如下: 在这里插入图片描述 需要将这三个工作簿中的分数这一列合并到一起,最终实现效果如下图: 在这里插入图片描述 2. ...

  4. mysql删除表中的唯一索引吗_Mysql 使用sql删除同表中重复数据并加唯一索引

    同一张表中,假设以两个字段做唯一业务,这两个字段分别为key1,key2, 则以这两个字段为唯一 DELETE tablename FROM tablename , ( SELECT min(id) ...

  5. sql表中只有子节点的递归_动态规划与静态规划、递归、分治、回溯

    动态规划算是运筹学或者算法中的硬骨头了.不是说算法本身有多难,而是学完用完之后还是感觉到对其领会的不够深入,一种能用其术,不知其道的感觉.在很多教材或者回答中,经常看多将动态规划放在递归这一部分中.当 ...

  6. sql 同一张表中两个记录不能共存_如何分析交易记录?

    [题目] 某商场为了分析用户购买渠道.表1是用户交易记录表,记录了用户id.交易日期.交易类型和交易金额. 表2是用户类型表,记录了用户支付类型(微信.支付宝.信用卡等),分别有type1.type2 ...

  7. 查询一个表中一个字段相同的数据_最实用MySQL 查询当天、本周,本月、上一个月的数据...

    MySQL 查询当天.本周,本月.上一个月的数据 mysql查询当天的所有信息: SELECT * FROM 表名 WHERE year(时间字段名)=year(now()) and month(时间 ...

  8. python列表所有元素平均值_【全网最简单Python教程】--10.列表元素的索引和返回索引值(Index函数使用)...

    在练习日4中,小鱼给大家讲述了神秘的ASCII码编译及解密过程. 在ASCII码中,字符与十进制数字的互相转换是通过 ord()函数 和 chr()函数. 今天小鱼要给大家介绍另一种在影视剧.侦探小说 ...

  9. python列表对应元素相乘_关于python:如何对两个列表进行元素明智的相乘?

    我想执行元素明智的乘法,将两个列表按值在Python中相乘,就像我们在Matlab中可以做到的那样. 这就是我在Matlab中要做的. 1 2 3a = [1,2,3,4] b = [2,3,4,5] ...

最新文章

  1. 漫画 | 如果面试时大家都说真话......
  2. Sqlserver UrlEncode
  3. Android 开发知识集合目录
  4. 微型计算机文献,微型计算机控制系统期刊文章参考文献 哪里有微型计算机控制系统参考文献...
  5. Swift调用微信支付宝SDK(Swift4.0)
  6. 字节跳动李航提出AMBERT!超越BERT!多粒度token预训练语言模型
  7. Android Design 1: Back键和Up键在App导航中的表现
  8. Centos 7 安装 java、搭建 Jenkins
  9. 移动电商平台弹性架构案例
  10. cdr三角形转化为圆角_cdr怎么把直角变成圆角
  11. Office2016专业版打开超链接时提示“您的组织策略阻止我们为您完成此操作。...”问题解决
  12. boost电路输出电流公式_​boost电路工作原理、参数计算、占空比
  13. 获取LOL所有在售皮肤的价格和发布日期
  14. 证明:$(g\circ f = e_X)\Rightarrow(g是满射)\wedge(f是单射)$
  15. Dell EMC VxRail超融合节点升级混合云软件
  16. GPON OMCI简介
  17. Dynamic Memory Networks DMN+
  18. 一键解决Conda安装某个库(如opencv)时conflict(冲突)的问题
  19. 计算机物理学论文300字,物理考试反思范文300字(精选6篇)
  20. 用python爬虫来登录深信服ac行为控制器,涉及到js加密部分,更新url分类库(针对企业微信更新)

热门文章

  1. qfp封装能够linux,QFP、PQFP、LQFP、TQFP封装形式及PCB详解
  2. PAT_B_1012 数字分类 (有待改进)
  3. TypeScript 联合类型(union type)
  4. 【USACO2006 Mar】滑雪缆车 skilift
  5. 01-JAVA语言基础
  6. python之路-SQLAlchemy
  7. 精通init ramfs构建
  8. SmartFoxServer学习总结(转载)
  9. 11小时 python自动化测试从入门到_从设计到开发Python接口自动化测试框架实战,资源教程下载...
  10. python图形化编程实验_转换图像RGB-实验室与python