Find frequencies of elements of an array present in another array
Last Updated :
30 Nov, 2021
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++14
// 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
// 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
# 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#
// 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
JavaScript
<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>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Map elements of an array to elements of another array Given two arrays A and B of positive integers, elements of array B can be mapped to elements of array A only if both the elements have same value. The task is to compute the positions in array A to which elements of array B will be mapped. Print NA if mapping for a particular element cannot be done.
6 min read
Find elements which are present in first array and not in second Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array. Examples : Input : a[] = {1, 2, 3, 4, 5, 10}; b[] = {2, 3, 1, 0, 5};Output : 4 10 4 and 10 are present in first array, butnot in second array.Input : a[] = {4, 3, 5, 9, 11}; b[]
14 min read
Check if an array is subset of another array Given two arrays a[] and b[] of size m and n respectively, the task is to determine whether b[] is a subset of a[]. Both arrays are not sorted, and elements are distinct.Examples: Input: a[] = [11, 1, 13, 21, 3, 7], b[] = [11, 3, 7, 1] Output: trueInput: a[]= [1, 2, 3, 4, 5, 6], b = [1, 2, 4] Output
13 min read
Elements of first array that have more frequencies Given two arrays (which may or may not be sorted). These arrays are such that they might have some common elements in them. We need to find elements whose counts of occurrences are more in first array than second. Examples: Input : ar1[] = {1, 2, 2, 2, 3, 3, 4, 5} ar2[] = {2, 2, 3, 3, 3, 4} Output :
7 min read
Find whether an array is subset of another array using Map Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both arrays are distinct.Examples: Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1} Output: arr2[] is a subset o
7 min read
Find whether an array is subset of another array using Map Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both arrays are distinct.Examples: Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1} Output: arr2[] is a subset o
7 min read