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

哈希表中能有相同元素吗

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

哈希表中能有相同元素吗

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380565.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汇编指令处理的数据长度

在8086CPU中&#xff0c;可以处理两种尺寸的数据&#xff0c;byte&#xff08;8位&#xff09;和word&#xff08;16位&#xff09;&#xff0c;所以要在指令中说明是字操作还是字节操作 通过寄存器名指明要处理数据的尺寸 字操作&#xff1a; mov ax,1 mov bx,ds:[0] inc ax…

oracle 数组的用法,oracle存储过程中数组的使用

create or replace package ArrayTestPKG1 istype tt_type is table of varchar(32) INDEX BY BINARY_INTEGER; --- 定义数组type table2 is table of tableA.columnA%type index by binary_integer;function toArray(Liststr in varchar, V1 out tt_type) return number;Proc…

XP的一些小结

XP的一些小结 最近博客上关于XP的讨论也有不少文章&#xff0c;也很火热&#xff0c;我觉得&#xff1a;与其象有的人说的&#xff0c;XP是否适应国情&#xff0c;倒不如书XP是否适应具体的项目&#xff0c;具体的开发小组&#xff0c;具体的环境。我想起了在大学毕业设计时&am…

python整数转换字符串_使用Python中的str()函数将整数值转换为字符串

python整数转换字符串Given an integer value and we have to convert the value to the string using str() function. 给定一个整数值&#xff0c;我们必须使用str()函数将该值转换为字符串。 Python code to convert an integer value to the string value Python代码将整数…

2011年:签到已死?

导读&#xff1a;作为移动互联网最受关注的热点之一&#xff0c;各式LBS应用一度大量涌现&#xff0c;但其发展局限也越来越被更多的业界同行清楚认知&#xff0c;LBS只是一个功能特性还是可以支撑起一个产品&#xff1f;签到如何添加黏性和用户核心需求结合&#xff1f;LBS厂商…

Pip:基本命令和使用的指南,实现有效的包管理

目录 学习目标&#xff1a; 学习内容&#xff1a; 学习时间&#xff1a; 学习产出&#xff1a; 介绍 Pip 工具&#xff1a;Pip 是 Python 包管理工具&#xff0c;可以帮助用户方便地安装、管理和升级 Python 包&#xff1a; 安装 Pip 工具&#xff1a;学习如何在不同操作系统上…

div指令

功能&#xff1a; 除法 格式&#xff1a; div 寄存器 div 内存单元注解 除数&#xff1a;有8位和16位两种&#xff0c;在寄存器或内存单元中被除数&#xff1a;如果除数为8位&#xff0c;被除数则为16位&#xff0c;默认放在AX中&#xff0c;如果除数为16位&#xff0c;被除数…

RCP的布局

如果按照网上的例子创建出来的rcp只是一个空框架&#xff0c;如果你想显示出在插件里正常工作的界面&#xff0c;你需要在你自己的透视图类的createInitialLayout方法里布局界面。 在窗口底部显示属性页&#xff1a; layout.addView("org.eclipse.ui.views.PropertySheet&…

oracle dg 搭建方式,Linux平台 Oracle 11g DG测试环境快速搭建参考

环境现状&#xff1a;两台虚拟主机A和B&#xff1a;1. A机器已安装ASM存储的Oracle 11g 实例2. B机器已安装系统&#xff0c;配置以及目录结构均和A机器保持一致/u01 3块ASM盘DG部署规划&#xff1a;primarystandby主机JY-DBJY-DBSdb_namejyzhaojyzhaodb_unique_namejyzhaojyz…

C#| 使用String.Format()方法将小数点后的数字四舍五入

To round the digits after the decimal point, we can use String.Format() method, here is the example. 为了将小数点后的数字四舍五入&#xff0c;我们可以使用String.Format()方法&#xff0c;这里是示例。 using System;namespace ConsoleApplication1{class Program{s…

SmartFoxServer学习总结(转载)

一、要安装pro类型版本&#xff0c;此类型版本支持的功能较多&#xff0c;我安装的是SmartFoxServerPRO_1.6.2二、需要java虚拟机支持&#xff0c;最好安装jre-6u7-windows-i586-p-s.exe,把Java\jre1.6.0_07文件夹下的所有文件复制&#xff0c;覆盖到SmartFoxServerPRO_1.6.2\j…

dup和dd指令

dup 功能&#xff1a; 和数据定义的伪指令配合使用&#xff0c;用来进行数据的重复 格式&#xff1a; 数据类型 重复的次数 dup &#xff08;重复的数据&#xff09; db 3 dup(0)相当于db 0,0,0;定义了3个字节&#xff0c;他们的字节都是0 db 3 dup(0,1,2)相当于 db 0,1,2&…

汇编指令(转)

一、数据传输指令  它们在存贮器和寄存器、寄存器和输入输出端口之间传送数据.  1. 通用数据传送指令.    MOV  传送字或字节.    MOVSX 先符号扩展,再传送.    MOVZX 先零扩展,再传送.    PUSH  把字压入堆栈.    POP  把字弹出堆栈.    …

oracle10g 克隆安装,克隆Oracle Home(10g2)

克隆一个已经存在的Oracle Home&#xff0c;免掉新安装oracle10g软件的痛苦&#xff0c;如果原Oracle Home已经安装了patch就省得打patch了。一、在目标主机172.19.111.37上做安装前准备工作1、增加组和用户(和克隆主机的目录结构一样)# groupadd oinstall# groupadd dba# user…

取地址符和解引用符的区别_(&)和解引用(*)运算符的地址以及C中的指针...

取地址符和解引用符的区别Here, we are discussing about the two most useful operators with the pointers, why and how they are used? 在这里&#xff0c;我们用指针讨论两个最有用的运算符 &#xff0c;为什么以及如何使用它们&#xff1f; 1)运营商地址(&#xff06;)…

汇编offset

功能&#xff1a; 取标号的偏移地址 assume cs:codesg codeseg segmentstart:mov ax,offset starts: mov ax,offset s codeseg ends end startmov ax,offset start 相当于mov ax,0&#xff0c;因为start是代码段中的标号&#xff0c;他所标记的指令是代码中的第一条指令&#…

細微之處看看mysql與sql server的一些差別

以前不怎麼使用mysql&#xff0c;最近有些事情&#xff0c;需要用到php和mysql。目前使用的版本是5.5.1 發現mysql與sql server有很多不同。可能逐漸地會整理一些文檔出來給大家參考。 今天第一篇說說&#xff0c;update操作的差異。在mysql中&#xff0c;如果update語句要設置…

php商品状态精品 热销,ecshop商品列表,商品详细页,热销,精品,搜索列表页调用商品销售量(已销售数量)...

ecshop各个页面调用商品销售量方法(原创可用)ECSHOP模板首页的推荐商品包括热销推荐和促销三个文件只对热销商品为例第一步&#xff1a;打开根目录/includes/lib_goods.php文件。在文件末尾添加方法function selled_count($goods_id){$sql "select sum(goods_number) as c…

c ++ stl_通过分配另一个列表的所有元素来创建列表| C ++ STL

c stlThere are two methods to implement it... 有两种方法可以实现它... 1 ) by assigning the existing list direct to the new list 1)通过直接将现有列表分配给新列表 list<int> list2 list1;2 ) by using list::assign() function 2)通过使用list :: assign(…

引用参数

• 默认情况下CLR规定所有方法的参数都按值传递的 • 参数类型分为:值类型参数和引用类型参数 • 参数传递方式:传值方式和传址方式 • Ref和Out的共同:传址传递 • Ref和Out的异同:ref修饰的参数必须在传递前对参数进行初始化;out反之,且必须在参数返回前进行初始化赋值 •在值…