给定两个大小分别为 M 和 N 的数组(可以排序也可以不排序)。有这样的数组,它们可能有一些共同的元素。您需要计算第一个数组中出现次数多于第二个的元素的数量。
例子:
Input : arr1[] = {41, 43, 45, 50}, M = 4
arr2[] = {55, 67, 65, 61, 62}, N = 5
Output : 4
Explanation:
arr1[] contains 41, 43, 45, 50 with frequency all having 1
arr2[] does not contain any such element which is common in both hence count will be 4
Input : arr1[] = {40, 45, 56, 60}, M = 4
arr2[] = {40, 45, 56, 58}, N = 4
Output : 1
Explanation:
arr1[] contains 40, 45, 56, 60 with frequency all having 1
arr2[] contains 3 elements in common with arr1[] which are 40, 45, 56
but not 60 hence count becomes 1
这个想法是使用哈希映射并保留第一个数组中数字频率的计数。在遍历第二个数组时,减少映射中遇到的每个整数的计数。现在计算频率大于 0 的元素,否则返回 0。
C++
// C++ to count number of elements present in arr1 whose
// occurrence is more than in arr2
#include
using namespace std;
int Largercount(int arr1[], int arr2[], int m, int n)
{
bool f = false;
int count = 0;
// map to store frequency of elements present in arr1
unordered_map mp;
// frequency of elements of arr1 is calculated
for (int i = 0; i < m; i++)
mp[arr1[i]]++;
// check if the elements of arr2
// is present in arr2 or not
for (int i = 0; i < n; i++)
if (mp.find(arr2[i]) != mp.end() && mp[arr2[i]] != 0)
mp[arr2[i]]--;
// count the elements of arr1 whose
// frequency is more than arr2
for (int i = 0; i < m; i++) {
if (mp[arr1[i]] != 0) {
count++;
mp[arr1[i]] = 0;
}
}
return count;
}
// Driver code
int main()
{
int arr1[] = { 2, 4, 4, 6, 6, 6, 8, 9 };
int arr2[] = { 2, 2, 4, 6, 6 };
cout << Largercount(arr1, arr2, 8, 5);
return 0;
}
Java
// Java to count number of elements present in arr1 whose
// occurrence is more than in arr2
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static int Largercount(int arr1[], int arr2[], int m, int n)
{
int count = 0;
// map to store frequency of elements present in arr1
HashMap mp = new HashMap();
// frequency of elements of arr1 is calculated
for (int i = 0; i < m; i++) {
int key = arr1[i];
if (mp.containsKey(key)) {
int freq = mp.get(key);
freq++;
mp.put(key, freq);
}
else
mp.put(key, 1);
}
// check if the elements of arr2 is present in arr2 or not
for (int i = 0; i < n; i++) {
if (mp.containsKey(arr2[i]) && mp.get(arr2[i]) != 0) {
int freq = mp.get(arr2[i]);
freq--;
mp.put(arr2[i], freq);
}
}
// count the elements of arr1 whose
// frequency is more than arr2
for (int i = 0; i < m; i++) {
if (mp.get(arr1[i]) != 0) {
count++;
mp.put(arr1[i], 0);
}
}
return count;
}
// Driver code
public static void main(String[] args)
{
int arr1[] = new int[] { 2, 4, 4, 6, 6, 6, 8, 9};
int arr2[] = new int[] { 2, 2, 4, 6, 6 };
System.out.print(Largercount(arr1, arr2, 8, 5));
}
}
Python3
# Python3 to count number of elements
# present in arr1 whose occurrence is
# more than in arr2
def Largercount(arr1, arr2, m, n):
count = 0
# map to store frequency of
# elements present in arr1
mp=dict()
# frequency of elements of arr1
# is calculated
for i in range(m):
mp[arr1[i]] = mp.get(arr1[i], 0) + 1
# check if the elements of arr2
# is present in arr2 or not
for i in range(n):
if (arr2[i] in mp.keys() and
mp[arr2[i]] != 0):
mp[arr2[i]] -= 1
# count the elements of arr1 whose
# frequency is more than arr2
for i in range(m):
if (mp[arr1[i]] != 0):
count += 1
mp[arr1[i]] = 0
return count
# Driver code
arr1 = [2, 4, 4, 6, 6, 6, 8, 9]
arr2 = [2, 2, 4, 6, 6 ]
print(Largercount(arr1, arr2, 8, 5))
# This code is contributed by mohit kumar
C#
// C# to count number of elements
// present in arr1 whose occurrence
// is more than in arr2
using System;
using System.Collections.Generic;
class GFG
{
public static int Largercount(int[] arr1,
int[] arr2,
int m, int n)
{
int count = 0;
// map to store frequency of
// elements present in arr1
Dictionary mp = new Dictionary();
// frequency of elements
// of arr1 is calculated
for (int i = 0; i < m; i++)
{
int key = arr1[i];
if (mp.ContainsKey(key))
{
int freq = mp[key];
freq++;
mp[key] = freq;
}
else
{
mp[key] = 1;
}
}
// check if the elements of arr2
// is present in arr2 or not
for (int i = 0; i < n; i++)
{
if (mp.ContainsKey(arr2[i]) &&
mp[arr2[i]] != 0)
{
int freq = mp[arr2[i]];
freq--;
mp[arr2[i]] = freq;
}
}
// count the elements of arr1 whose
// frequency is more than arr2
for (int i = 0; i < m; i++)
{
if (mp[arr1[i]] != 0)
{
count++;
mp[arr1[i]] = 0;
}
}
return count;
}
// Driver code
public static void Main(string[] args)
{
int[] arr1 = new int[] {2, 4, 4, 6,
6, 6, 8, 9};
int[] arr2 = new int[] {2, 2, 4, 6, 6};
Console.Write(Largercount(arr1, arr2, 8, 5));
}
}
// This code is contributed by Shrikant13
Javascript
4
时间复杂度: O(m + n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。