![]() |
VOOZH | about |
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:
3 4 1
Time Complexity: O(N)
Auxiliary Space: O(N)