Skip to content

Commit 07877d2

Browse files
committed
HASHMAP implementation
1 parent 7b56bfe commit 07877d2

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/1
2+
3+
#include <bits/stdc++.h>
4+
#include <unordered_set>
5+
using namespace std;
6+
7+
// int getPairsCount(int arr[], int n, int k)
8+
// {
9+
// int count = 0 ;
10+
// for (int i = 0; i < n; i++)
11+
// {
12+
// int required_number = k - arr[i];
13+
14+
// }
15+
// return count ;
16+
// }
17+
18+
int main()
19+
{
20+
int arr[] = {1, 5, 7, 1}; // input array
21+
int n = sizeof(arr) / sizeof(arr[0]); // size of an array
22+
//int k = 6; // sum we need
23+
cout<<mp[9] ;
24+
//cout << getPairsCount(arr, n, k);
25+
return 0;
26+
}

frequency_of_elements.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int arr[] = {1, 2, 2, 2, 4, 1, 1, 7, 8, 7, 2, 9, 4, 4, 8, 8, 8, 8, 1};
7+
int n = sizeof(arr) / sizeof(arr[0]);
8+
unordered_map<int, int> freq;
9+
for (int i = 0; i < n; i++)
10+
{
11+
freq[arr[i]]++;
12+
}
13+
// map use Red Black tree n(logn)
14+
// map<int,int>::iterator it ;
15+
// unordered map takes hashing table n(1)
16+
unordered_map<int, int>::iterator it;
17+
for (it = freq.begin(); it != freq.end(); it++)
18+
{
19+
cout << it->first << "->" << it->second << endl;
20+
}
21+
22+
return 0;
23+
}

0 commit comments

Comments
 (0)