Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views17 pages

Lab Manual Hasing Problems

Uploaded by

subhash.chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

Lab Manual Hasing Problems

Uploaded by

subhash.chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

LAB MANUAL

Hashing

Find the Frequency


Difficulty: EasyAccuracy: 77.19%Submissions: 79K+Points: 2

Given an Array Arr of N positive integers and an integer X. Return


the frequency of X in the array.

Example 1:

Input:
N = 5
Arr = {1, 1, 1, 1, 1}
X = 1
Output:
5
Explanation: Frequency of 1 is 5.

Example 2:

Input:
N = 6
Arr = {1, 2, 3, 3, 2, 1}
X = 2
Output:
2
Explanation: Frequency of 2 is 2.

Time Complexity: O(N)


Space Complexity: O(1)
Your Task:
Your task is to complete the function findFrequency() which should return the
frequency of X.

Constraints:
1 <= N <= 105
1 <= Arr[i] <= 105
1 <= X <= 105

//{ Driver Code Starts

//Initial Template for C++

#include <bits/stdc++.h>

using namespace std;

// } Driver Code Ends

//User function Template for C++

/*Function to find frequency of x

* x : element whose frequency is to be found

* v : input vector

*/
int findFrequency(vector<int> arr, int x){

// Your code here

unordered_map<int, int> mp;

// Traverse through array elements and

// count frequencies

for (int i = 0; i < arr.size(); i++)

mp[arr[i]]++;

// Traverse through map and print frequencies

for (auto i : mp)

if(i.first == x)

return i.second;

//{ Driver Code Starts.

int main() {

int testcase;

cin >> testcase;


while(testcase--){

int N;

cin >> N;

// Declaring vector

vector<int> Arr;

// Taking vector element input

for(int i = 0;i<N;i++){

int k;

cin >> k;

Arr.push_back(k);

// element whose frequency to be find

int X;

cin >> X;

cout << findFrequency(Arr, X) << endl;

return 0;

}
// } Driver Code Ends

Non-Repeating Element
Difficulty: EasyAccuracy: 39.31%Submissions: 112K+Points: 2
Find the first non-repeating element in a given array arr of integers and if
there is not present any non-repeating element then return 0

Note: The array consists of only positive and negative integers and not zero.

Examples:

Input: arr[] = [-1, 2, -1, 3, 2]


Output: 3
Explanation: -1 and 2 are repeating whereas 3 is
the only number occuring once. Hence, the output is
3.
Input: arr[] = [1, 1, 1]
Output: 0
Explanation: There is not present any non-repeating
element so answer should be 0.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(n).

Constraints:
1 <= arr.size <= 106
-109 <= arr[i] <= 109
arr[i] != 0

//{ Driver Code Starts

#include <bits/stdc++.h>

using namespace std;


// } Driver Code Ends

class Solution {

public:

int firstNonRepeating(vector<int>& arr) {

// Complete the function

unordered_map<int, int> mp;

for(int i=0; i<arr.size(); i++){

mp[arr[i]]++;

for(auto itr : mp){

if(itr.second == 1){

return itr.first;

return 0;

};

//{ Driver Code Starts.


int main() {

string ts;

getline(cin, ts);

int t = stoi(ts);

while (t--) {

string line;

getline(cin, line);

stringstream ss(line);

vector<int> nums;

int num;

while (ss >> num) {

nums.push_back(num);

Solution ob;

cout << ob.firstNonRepeating(nums) << endl;

// } Driver Code Ends

Frequencies of Limited Range Array Elements


Difficulty: EasyAccuracy: 27.64%Submissions: 303K+Points: 2

You are given an array arr[] containing positive integers. These integers can be from 1
to p, and some numbers may be repeated or absent. Your task is to count the
frequency of all numbers that lie in the range 1 to n.
Note:

1. Do modify the array in-place changes in arr[], such that arr[i] = frequency(i)
and assume 1-based indexing.
2. The elements greater than n in the array can be ignored when counting.
Examples

Input: n = 5, arr[] = [2, 3, 2, 3, 5], p = 5


Output: [0, 2, 2, 0, 1]
Explanation: Counting frequencies of each array element
We have: 1 occurring 0 times. 2 occurring 2 times. 3
occurring 2 times. 4 occurring 0 times. 5 occurring 1
time, all the modifications done in the same given arr[].
Input: n = 4, arr[] = [3, 3, 3, 3], p = 3
Output: [0, 0, 4, 0]
Explanation: Counting frequencies of each array element
We have: 1 occurring 0 times. 2 occurring 0 times. 3
occurring 4 times. 4 occurring 0 times.
Input: n = 2, arr[] = [8, 9], p = 9
Output: [0, 0]
Explanation: Counting frequencies of each array element
We have: 1 occurring 0 times. 2 occurring 0 times. Since
here P=9, but there are no 9th Index present so can't
count the value.
Expected time complexity: O(n)
Expected auxiliary space: O(1)

Constraints:
1 ≤ n ≤ 105
1 ≤ p ≤ 4*104
1 <= arr[i] <= p
Try more examples
//{ Driver Code Starts

#include <bits/stdc++.h>

using namespace std;

// } Driver Code Ends

class Solution {

public:

// Function to count the frequency of all elements from 1 to N in the array.

void frequencyCount(vector<int>& arr, int N, int P) {

// do modify in the given array

unordered_map<int, int> mp;

for(int i=0; i<N; i++){

if(arr[i] >= 1 && arr[i] <= N){

mp[arr[i]]++;

for(int i=0; i<N; i++){

arr[i] = mp[i+1];

};
//{ Driver Code Starts.

int main() {

long long t;

// testcases

cin >> t;

while (t--) {

int N, P;

// size of array

cin >> N;

vector<int> arr(N);

// adding elements to the vector

for (int i = 0; i < N; i++) {

cin >> arr[i];

cin >> P;

Solution ob;

// calling frequncycount() function

ob.frequencyCount(arr, N, P);
// printing array elements

for (int i = 0; i < N; i++)

cout << arr[i] << " ";

cout << endl;

return 0;

// } Driver Code Ends


class Solution {

// Function to count the frequency of all elements from 1 to N in the


array.

public static void frequencyCount(int arr[], int N, int P) {

// do modify in the given array

HashMap<Integer, Integer> map = new HashMap<>();

for(int i=0; i<arr.length; i++){

if(arr[i]<=N){

if(!map.containsKey(arr[i])){

map.put(arr[i], 1);

}else{

map.put(arr[i], map.get(arr[i])+1);

for(int i=1; i<=N; i++){


if(map.containsKey(i)){

arr[i-1] = map.get(i);

}else{

arr[i-1] = 0;

}
}
Frequency of each character in a String using
unordered_map

Given a string str, the task is to find the frequency of each character of a string using
an unordered_map in C++ STL.
Examples:
Input: str = “geeksforgeeks”
Output:
r1
e4
s2
g2
k2
f1
o1
Input: str = “programming”
Output:
n1
i1
p1
o1
r2
a1
g2
m2
Approach:
1. Traverse each character of the given string str.
2. Check whether the current character is present in unordered_map or not.
3. If it is present, then update the frequency of the current characters else insert the
characters with frequency 1 as shown below:
if(M.find(s[i])==M.end()) {
M.insert(make_pair{s[i], 1});
}
else {
M[s[i]]++;
}
4. Traverse the unordered_map and print the frequency of each characters
stored as a mapped value.
Below is the implementation of the above approach:

 CPP

// C++ program for the above approach

#include <bits/stdc++.h>

using namespace std;

void printFrequency(string str)

// Define an unordered_map
unordered_map<char, int> M;

// Traverse string str check if

// current character is present

// or not

for (int i = 0; str[i]; i++)

// If the current characters

// is not found then insert

// current characters with

// frequency 1

if (M.find(str[i]) == M.end())

M.insert(make_pair(str[i], 1));

// Else update the frequency

else

M[str[i]]++;
}

// Traverse the map to print the

// frequency

for (auto& it : M) {

cout << it.first << ' ' << it.second << '\n';

// Driver Code

int main()

string str = "geeksforgeeks";

// Function call

printFrequency(str);

return 0;

Output
r 1
e 4
s 2
g 2
k 2
f 1
o 1

You might also like