perl 哈希数组的哈希

Prerequisite: Hashing data structure

先决条件: 哈希数据结构

Problem statement:

问题陈述:

Check whether two arrays are similar or not using the hash table. The arrays are of the same size.

使用哈希表检查两个数组是否相似。 数组的大小相同。

Example:

例:

arr1= [1, 2, 1, 3, 2, 1]
arr2= [2, 2, 3, 1, 1, 1]

Solution:

解:

There are several ways to solving this problem and one is by sorting both of the array. Then we can check elements one by one and if the two arrays are similar, it has to match for every single element. So, after sorting, a[i] must be b[i] for each i. But the method we will discuss is hashing which computes more easily.

解决此问题的方法有几种,一种是对两个数组进行排序。 然后我们可以一一检查元素,如果两个数组相似,则必须为每个元素匹配。 因此,排序后,每个i的 a [i]必须是b [i] 。 但是我们将讨论的方法是散列,它更容易计算。

The approach is to create two separate hash tables for each array. If the hash tables are exactly same then we can say that the arrays are exactly same.

该方法是为每个数组创建两个单独的哈希表。 如果哈希表完全相同,那么我们可以说数组完全相同。

So how can we create the hash tables and what will be the hash function?

那么我们如何创建哈希表,哈希函数将是什么呢?

Okay, so here each element is our key and the hash table has size of the range of the array. So, if the range of the array is [0,99] then the hash table size would be 100.
What will be our hash function and how would we map the keys to the corresponding location in the hash table?

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

The hash function h(x)=x here but instead of storing the key itself using linear probing we will keep the frequency (this is same as the number of collisions) 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, using the above hash function, we will create two separate hash tables for two arrays (The hash function will remain the same for both)

因此,使用上面的哈希函数,我们将为两个数组创建两个单独的哈希表(哈希函数对于两个数组将保持相同)

So the algorithm will be,

因此算法将是

Step 1:

第1步:

Create the hash table like below:
Initially hash tables are empty (Hash1 & Hash2)
For each key in input array arr1:
Hash1[key]++
For each key in input array arr2:
Hash2[key]++

Step 2:

第2步:

Compare each location of the hash table
If all locations have the same value (same frequency for each unique key)
then the two arrays are the same otherwise not.

Dry run with the example:

空运行示例:

arr1= [1, 2, 1, 3, 2, 1]
arr2= [2, 2, 3, 1, 1, 1]
After creating the hash table as step1 we will have
Hash1:
Index   Value
1   3
2   2
3   1
Hash2:
Index   Value
1   3
2   2
3   1
So,
both hash1 and hash 2 is exactly
the same and thus the arrays are same too.
Another example where arrays are not exactly same,
arr1= [1, 2, 1, 3, 2, 3]
arr2= [2, 2, 3, 1, 1, 1]
After creating the hash table as step1 we will have
Hash1:
Index   Value
1   2
2   2
3   2
Hash2:
Index   Value
1   3
2   2
3   1
Since location 1 has different values for hash1 and hash2
we can conclude the arrays are not the same.
So, what we actually did using hashing is to check
whether all the unique elements in the two arrays are exactly
the same or not and if the same then their
frequencies are the same or not.

C++ implementation:

C ++实现:

//C++ program to check if two arrays
//are equal or not
#include <bits/stdc++.h>
using namespace std;
bool similar_array(vector<int> arr1, vector<int> arr2)
{
//create teo different hash table where for each key
//the hash function is h(arr[i])=arr[i]
//we will use stl map as hash table and
//will keep frequency stored
//so say two keys arr[0] and arr[5] are mapping to
//the same location, then the location will have value 2
//instead of the keys itself
//if two hash tables are exactly same then
//we can say that our arrays are similar
map<int, int> hash1;
map<int, int> hash2;
//for each number
for (int i = 0, j = 0; i < arr1.size(); i++, j++) {
hash1[arr1[i]]++;
hash2[arr2[i]]++;
}
//now check whether hash tables are exactly same or not
for (auto it = hash1.begin(), ij = hash2.begin(); it != hash1.end() && ij != hash2.end(); it++, ij++) {
if (it->first != ij->first || it->second != ij->second)
return false;
}
//so ans will be updated maxdiff
return true;
}
int main()
{
int n, m;
cout << "Enter number of elements for array1 & array2\n";
cin >> n;
//arrays having equal sum
vector<int> arr1(n, 0);
vector<int> arr2(n, 0);
cout << "Input the array elements\n";
for (int i = 0; i < n; i++) {
cin >> arr1[i];
cin >> arr2[i];
}
if (similar_array(arr1, arr2))
cout << "The arrys are equal";
else
cout << "The arrys are not equal";
return 0;
}

Output:

输出:

Enter number of elements for array1 & array2
6
Input the array elements
1 2 1 3 2 1
2 2 3 1 1 1
The arrys are equal

翻译自: https://www.includehelp.com/data-structure-tutorial/check-if-two-arrays-are-similar-or-not-using-hashing.aspx

perl 哈希数组的哈希

perl 哈希数组的哈希_使用哈希检查两个数组是否相似相关推荐

  1. 如何快速找出找出两个数组中的_找出JavaScript中两个数组之间的差异

    LeetCode今天面临的挑战是在数组中查找所有消失的数字. 蛮力 我们的输入包括一个缺少数字的实际数组.我们想将该数组与相同长度的数组进行比较,其中没有遗漏的数字.所以如果给定的话[4,3,2,7, ...

  2. 数组越界怎么判断_算法连载之求解两个有序数组的中位数

    问题 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2.找出这两个有序数组的中位数.假设 nums1 和 nums2 不会同时为空. 示例 1: nums1 = [1, 3] num ...

  3. python拼接两个数组_在Python中连接两个数组

    根据你的描述,你的案子看起来像:In [344]: a=[np.arange(5),np.arange(5)] In [345]: b=[np.arange(5),np.arange(3)] In [ ...

  4. 两个数组结果相减_学点算法(三)——数组归并排序

    今天来学习归并排序算法. 分而治之 归并算法的核心思想是分而治之,就是将大问题转化为小问题,在解决小问题的基础上,再去解决大问题.将这句话套用到排序中,就是将一个大的待排序区间分为小的待排序区间,对小 ...

  5. c++删除数组中重复元素_在VBA中如何使用动态数组,以及利用动态数组去除重复值的方法...

    大家好,我们今日继续讲解VBA数组与字典解决方案第22讲:在VBA中如何使用动态数组,以及利用动态数组去除重复值的方法.如果文本中含有大量的重复值,此时,如果我们要剔除重复值,该怎么办?用VBA的方法 ...

  6. vba数组如何精确筛选_第22讲:利用动态数组去除重复值的方法

    大家好,我们今日继续讲解VBA数组与字典解决方案第22讲:在VBA中如何使用动态数组,以及利用动态数组去除重复值的方法.如果文本中含有大量的重复值,此时,如果我们要剔除重复值,该怎么办?用VBA的方法 ...

  7. php两个数组找公共部分,PHP开发中如何查找两个数组的交集

    在PHP中,可以使用内置函数array_intersect()来查找两个数组的交集,它可以返回两个数组的公共元素(相交部分).下面我们就来具体介绍一下. array_intersect()函数 基本语 ...

  8. python中序列类型和数组之间的区别_「Python」序列构成的数组

    一.Python 标准库的序列类型分为: 容器序列: 能够存放不同类型数据的序列(list.tuple.collections.deque). 扁平序列: 只能容纳一种类型的数据(str.bytes. ...

  9. java在数组中放入随机数_如何在Java中随机播放数组

    java在数组中放入随机数 There are two ways to shuffle an array in Java. 有两种方法可以在Java中随机播放数组. Collections.shuff ...

最新文章

  1. 洛谷 P1142 轰炸
  2. java正则截取xml节点_实例讲述Java使用正则表达式截取重复出现的XML字符串功能...
  3. .NetCore Docker
  4. Android拖拽详解
  5. 流式计算框架Storm 编程案例部署Linux结果演示及pom依赖
  6. go context之WithDeadline的使用
  7. java8 streams_Java 8 Streams API作为友好的ForkJoinPool外观
  8. 软件工程--第一周学习进度
  9. ERROR: Process pool report error: Can‘t pickle
  10. 2021年 win10 Flash 不可用解决方法
  11. java 打包exe_Java项目打包成exe的详细教程
  12. 【小程序】安卓端InnerAudioContext无法销毁
  13. 精彩博文收集目录索引
  14. 开源网络蜘蛛(Spider)一览
  15. 源于小程序智能名片的两点思考
  16. “没有40K当什么程序员?”
  17. 分布式消息队列基础知识
  18. 3道题彻底搞定:套路解决递归问题
  19. 阿里2017校园招聘电话面试总结
  20. Commons-Collections简介

热门文章

  1. 电脑显示无法连接sql服务器,他人的电脑为什么无法连接我电脑上的sql sever服务器...
  2. linux下调用python脚本,Linux下QT调用Python脚本的解决方案,Qt,python,一种,解决办法
  3. HTML音乐播放没声音,网页没有声音但系统显示有声音怎么回事?如何解决?
  4. .net mysql查询数据库连接_asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)...
  5. python打出由边框包围的_python opencv 图像边框(填充)添加及图像混合的实现方法(末尾实现类似幻灯片渐变的效果)...
  6. 计算机护理职称考试报名时间2015,护理职称考试怎么报名?
  7. MySQL(7)索引
  8. python 文档字符串_新款Python文档字符串生成器来了
  9. Spark-大规模数据处理计算引擎
  10. 日常问题——pdsh localhost Connection refused