查找另一个数组中存在的数组元素的频率
给定两个大小分别为N和M的数组arr[]和brr[] ,任务是在arr[]中找到数组brr[]的元素的频率。
例子:
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[]
方法:通过将数组arr[]元素的频率存储在哈希图中,可以轻松解决该任务。遍历数组brr[]并检查它是否存在于 hashmap 中,并存储相应的频率。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
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 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 occ=new HashMap();
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 occ = new Dictionary();
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
输出
3 4 1
时间复杂度:O(N)
辅助空间:O(N)