Given two arrays arr[] and brr[] of sizes N and M respectively, the task is to find the frequencies of the elements of array brr[] in arr[].
Examples:
Input: N = 8, arr[] = {29, 8, 8, 8, 7, 7, 8, 7}, M = 3, brr[] = {7, 8, 29}
Output: {3, 4, 1}
Explanation: Frequencies of 7, 8 and 29 are 3, 4 and 1 respectively in arr[]
Input: arr[] = N = 6, {4, 5, 6, 5, 5, 3}, M = 3, brr[] = {1, 2, 3}
Output: {0, 0, 1}
Explanation: Frequencies of 1, 2 and 3 are 0, 0 and 1 respectively in arr[]
Approach: The task can easily be solved by storing the frequencies of elements of array arr[] in a hashmap. Iterate over the array brr[] and check if it is present in hashmap or not, and store the corresponding frequency.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the frequencies of // elements of brr[] in array arr[] void solve( int arr[], int brr[], int N, int M)
{ // Stores the frequency of elements
// of array arr[]
unordered_map< int , int > occ;
for ( int i = 0; i < N; i++)
occ[arr[i]]++;
// Iterate over brr[]
for ( int i = 0; i < M; i++) {
// Check if brr[i] is present in
// occ or not
if (occ.find(brr[i]) != occ.end()) {
cout << occ[brr[i]] << " " ;
}
else {
cout << 0 << " " ;
}
}
} // Driver Code int main()
{ int N = 8;
int arr[N] = { 29, 8, 8, 8, 7, 7, 8, 7 };
int M = 3;
int brr[M] = { 7, 8, 29 };
solve(arr, brr, N, M);
return 0;
} |
// Java program for the above approach import java.util.*;
class GFG{
// Function to find the frequencies of // elements of brr[] in array arr[] static void solve( int arr[], int brr[], int N, int M)
{ // Stores the frequency of elements
// of array arr[]
HashMap<Integer,Integer> occ= new HashMap<Integer,Integer>();
for ( int i = 0 ; i < N; i++)
{
if (occ.containsKey(arr[i])){
occ.put(arr[i], occ.get(arr[i])+ 1 );
}
else {
occ.put(arr[i], 1 );
}
}
// Iterate over brr[]
for ( int i = 0 ; i < M; i++) {
// Check if brr[i] is present in
// occ or not
if (occ.containsKey(brr[i])) {
System.out.print(occ.get(brr[i])+ " " );
}
else {
System.out.print( 0 + " " );
}
}
} // Driver Code public static void main(String[] args)
{ int N = 8 ;
int arr[] = { 29 , 8 , 8 , 8 , 7 , 7 , 8 , 7 };
int M = 3 ;
int brr[] = { 7 , 8 , 29 };
solve(arr, brr, N, M);
} } // This code is contributed by 29AjayKumar |
# Python3 program for the above approach # Function to find the frequencies of # elements of brr[] in array arr[] def solve(arr, brr, N, M) :
# Stores the frequency of elements
# of array arr[]
occ = dict .fromkeys(arr, 0 );
for i in range (N) :
occ[arr[i]] + = 1 ;
# Iterate over brr[]
for i in range (M) :
# Check if brr[i] is present in
# occ or not
if brr[i] in occ :
print (occ[brr[i]], end = " " );
else :
print ( 0 , end = " " );
# Driver Code if __name__ = = "__main__" :
N = 8 ;
arr = [ 29 , 8 , 8 , 8 , 7 , 7 , 8 , 7 ];
M = 3 ;
brr = [ 7 , 8 , 29 ];
solve(arr, brr, N, M);
# This code is contributed by AnkThon
|
// C# program for the above approach using System;
using System.Collections.Generic;
class GFG
{ // Function to find the frequencies of
// elements of brr[] in array arr[]
static void solve( int [] arr, int [] brr, int N, int M)
{
// Stores the frequency of elements
// of array arr[]
Dictionary< int , int > occ = new Dictionary< int , int >();
for ( int i = 0; i < N; i++)
{
if (occ.ContainsKey(arr[i]))
{
occ[arr[i]] = occ[arr[i]] + 1;
}
else
{
occ[arr[i]] = 1;
}
}
// Iterate over brr[]
for ( int i = 0; i < M; i++)
{
// Check if brr[i] is present in
// occ or not
if (occ.ContainsKey(brr[i]))
{
Console.Write(occ[brr[i]] + " " );
}
else
{
Console.Write(0 + " " );
}
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 8;
int [] arr = { 29, 8, 8, 8, 7, 7, 8, 7 };
int M = 3;
int [] brr = { 7, 8, 29 };
solve(arr, brr, N, M);
}
} // This code is contributed by Saurabh Jaiswal |
<script> // Javascript program for the above approach // Function to find the frequencies of // elements of brr[] in array arr[] function solve(arr, brr, N, M) {
// Stores the frequency of elements
// of array arr[]
let occ = new Map();
for (let i = 0; i < N; i++) {
if (occ.has(arr[i])) {
occ.set(arr[i], occ.get(arr[i]) + 1)
} else {
occ.set(arr[i], 1)
}
}
// Iterate over brr[]
for (let i = 0; i < M; i++) {
// Check if brr[i] is present in
// occ or not
if (occ.has(brr[i])) {
document.write(occ.get(brr[i]) + " " );
}
else {
document.write(0 + " " );
}
}
} // Driver Code let N = 8; let arr = [ 29, 8, 8, 8, 7, 7, 8, 7 ]; let M = 3; let brr = [ 7, 8, 29 ]; solve(arr, brr, N, M); // This code is contributed by gfgking. </script> |
3 4 1
Time Complexity: O(N)
Auxiliary Space: O(N)