Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashing using arrays and maps #630

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Hashing_using_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Hashing is a technique to find out the frequency of an element in an array at O(N) time complexity.
// This method uses hash array to perform hasing.
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; //size of array
cin>>n;
int max_input=INT_MIN;
int nums[n]; //array to be hashed
for(int i=0;i<n;i++){
cin>>nums[i];
max_input=max(max_input,nums[i]);
}
int hash[max_input+1]={0};
for(int i=0;i<n;i++){
hash[nums[i]]++;
}
int desired_num;
cin>>desired_num;
cout<<"The frequency of "<<desired_num<<" is "<<hash[desired_num];
}
19 changes: 19 additions & 0 deletions Hashing_using_map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; //size of array
cin>>n;
int nums[n]; //array to be hashed
for(int i=0;i<n;i++){
cin>>nums[i];
}
map<int,int> hashmap;
for(int i=0;i<n;i++){
hashmap[nums[i]]++;
}
int desired_num;
cin>>desired_num;
cout<<"The frequency of "<<desired_num<<" is "<<hashmap[desired_num];
}