日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/1 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 哈希表中能有相同元素吗_最小删除以使用哈希表使所有元素相同 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

哈希表中能有相同元素嗎

Prerequisite: Hashing data structure

先決條件: 哈希數(shù)據(jù)結(jié)構(gòu)

Problem statement:

問題陳述:

Find minimum number of deletions to make all elements same.

找到最小的刪除數(shù)以使所有元素相同。

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.

輸入數(shù)組= [12、13、4、12、12、15、12、17]
刪除的最小數(shù)量為4。刪除4個(gè)元素后,我們將得到數(shù)組[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.

我們可以使用哈希映射(哈希表)解決此問題。 一件事很清楚,要實(shí)現(xiàn)最少的刪除,我們需要保留出現(xiàn)次數(shù)最多的元素,并需要?jiǎng)h除其余的數(shù)組元素。 因此,我們的目標(biāo)是找到頻率最高的陣列元素。 找到數(shù)組元素后,我們將刪除其他元素,這就是我們的答案。

So how can we design the problem with the hash table and what will be the hash function?

那么我們?nèi)绾卧O(shè)計(jì)哈希表的問題以及哈希函數(shù)是什么呢?

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?

好的,所以這里的每個(gè)元素都是我們的鍵,哈希表具有數(shù)組范圍的大小。 因此,如果數(shù)組的范圍為[0,15],則哈希表大小將為15 。 我們的哈希函數(shù)將是什么?如何將鍵映射到哈希表中的對(duì)應(yīng)位置?

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.

此處的哈希函數(shù)h(x)= x ,而不是使用線性探測(cè)來存儲(chǔ)密鑰本身,我們將僅保留計(jì)數(shù),因?yàn)槭褂镁€性探測(cè)沒有意義,因?yàn)槊總€(gè)唯一的密鑰都將具有唯一的位置。

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

因此,例如,如果我們有三個(gè)2作為密鑰,則哈希表將在位置2存儲(chǔ)計(jì)數(shù)(頻率)。

So, after the hash table is created we can easily find the most occurred elements by each location (index) of the hash table.

因此,在創(chuàng)建哈希表之后,我們可以輕松地按哈希表的每個(gè)位置(索引)找到出現(xiàn)次數(shù)最多的元素。

So the algorithm will be:

因此,算法將是:

Step 1:

第1步:

Create the hash table like below, Initially hash table is emptyFor 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:

空運(yùn)行示例:

Input array is = [12, 13 ,4, 12, 12, 15, 12, 17] So hash table size would be (17-4)=13After creating the hash table as step1 we will have, Index Value 4 1 12 4 13 1 15 1 17 1So max_freq = 4 Hence, minimum deletion needed = 8-4

C++ implementation:

C ++實(shí)現(xiàn):

// 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 itselfmap<int, int> hash;for (auto i : arr) { //for each numberhash[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 frequencyint 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-maxfreqreturn 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

哈希表中能有相同元素嗎

總結(jié)

以上是生活随笔為你收集整理的哈希表中能有相同元素吗_最小删除以使用哈希表使所有元素相同的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。