perl 哈希数组的哈希
Prerequisite: Hashing data structure
先决条件: 哈希数据结构
Problem statement:
问题陈述:
Find the first element occurring K times in the array.
查找数组中出现K次的第一个元素。
Example:
例:
Input array= [2, 3, 2, 3, 2, 3]
K=3
The first element occurring k times is 2.
Though both 2 and 3 have occurred 3 times since 2 came first,
so the answer would be 2
Input array= [1 2 3 1 2]
K=3
None of the elements has occurrences 3,
so the answer would be -1.
Solution:
解:
We can solve this problem using a hash map (hash table). One thing is clear that to find out the first element occurring k times we need to keep track of frequencies for each unique key.
我们可以使用哈希映射(哈希表)解决此问题。 很明显,要找出出现k次的第一个元素,我们需要跟踪每个唯一键的频率。
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,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 only as we only require that.
这里的哈希函数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 check the frequency for each element starting from left to right, one by one. The one satisfying the constraint, i.e., having frequency k will be our answer. In this way, we will get the first element occurring k times. And if none of the elements satisfies then the answer would be -1.
我们可以从左到右逐一检查每个元素的频率。 满足约束,即具有频率k的那个将是我们的答案。 这样,我们将获得第一个出现k次的元素。 如果所有元素都不满足,则答案将为-1。
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步:
For each element e:
if(hash[e]==k)
return e;
End for
Dry run with the example:
空运行示例:
Input array is = [2, 3, 2, 3, 2, 3]
K=3
After creating the hash table as step1 we will have
Index Value
2 3
3 3
Now step2
While scanning for first element having frequency K
2 has frequency 3 and thus we return 3 immediately.
For example 2:
Input array= [1 2 3 1 2]
K=3
After creating the hash table as step1 we will have
Index Value
1 2
2 2
3 1
So after scanning we find that none of the elements
has frequency 3, thus answer will be -1.
C++ implementation:
C ++实现:
// C++ program to find first element
// occurring k times in the array
#include <bits/stdc++.h>
using namespace std;
int first_element(vector<int> arr, int n, int k)
{
//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 each number
for (auto i : arr) {
hash[i]++;
}
//now for any number if we find
//it has frequency k, we simply return
for (auto i : arr) {
if (hash[i] == k)
return i;
}
//otherwise return -1
return -1;
}
int main()
{
int n, k;
cout << "Enter number of elements\n";
cin >> n;
cout << "Enter k\n";
cin >> k;
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 operations required to make all elements same is: ";
cout << first_element(arr, n, k) << endl;
return 0;
}
Output:
输出:
RUN 1:
Enter number of elements
6
Enter k
3
Input the array elements
2 3 2 3 2 3
Minimum number of operations required to make all elements same is: 2
RUN 2:
Enter number of elements
5
Enter k
3
Input the array elements
1 2 3 1 2
Minimum number of operations required to make all elements same is: -1
perl 哈希数组的哈希