给定两个数组(可能排序也可能不排序)。这些数组中可能包含一些公共元素。我们需要找到在第一个数组中出现次数多于第二个的元素。
例子:
Input : ar1[] = {1, 2, 2, 2, 3, 3, 4, 5}
ar2[] = {2, 2, 3, 3, 3, 4}
Output : 1 2 5
1 occurs one times in first and zero times in second
2 occurs three times in first and two times in second
............................
Input : ar1[] = {1, 3, 4, 2, 3}
ar2[] = {3, 4, 5}
Output : 3
这个想法是使用散列。我们遍历第一个数组并将所有元素及其频率插入哈希表中。现在我们遍历第二个数组并减少哈希表中公共元素的频率。现在我们再次遍历第一个数组并打印那些频率仍然大于 0 的元素。 为了避免重复打印相同的元素,我们将频率设置为 0。
C++
// C++ program to print all those elements of
// first array that have more frequencies than
// second array.
#include
using namespace std;
// Compares two intervals according to staring times.
void moreFreq(int ar1[], int ar2[], int m, int n)
{
// Traverse first array and store frequencies
// of all elements
unordered_map mp;
for (int i = 0; i < m; i++)
mp[ar1[i]]++;
// Traverse second array and reduce frequencies
// of common elements.
for (int i = 0; i < n; i++)
if (mp.find(ar2[i]) != mp.end())
mp[ar2[i]]--;
// Now traverse first array again and print
// all those elements whose frequencies are
// more than 0. To avoid repeated printing,
// we set frequency as 0 after printing.
for (int i = 0; i < m; i++) {
if (mp[ar1[i]] > 0) {
cout << ar1[i] << " ";
mp[ar1[i]] = 0;
}
}
}
// Driver code
int main()
{
int ar1[] = { 1, 2, 2, 2, 3, 3, 4, 5 };
int ar2[] = { 2, 2, 3, 3, 3, 4 };
int m = sizeof(ar1) / sizeof(ar1[0]);
int n = sizeof(ar2) / sizeof(ar2[0]);
moreFreq(ar1, ar2, m, n);
return 0;
}
Java
// Java program to print all those elements of
// first array that have more frequencies than
// second array.
import java.util.*;
class GFG
{
// Compares two intervals according to staring times.
static void moreFreq(int ar1[], int ar2[], int m, int n)
{
// Traverse first array and store frequencies
// of all elements
Map mp = new HashMap<>();
for (int i = 0 ; i < m; i++)
{
if(mp.containsKey(ar1[i]))
{
mp.put(ar1[i], mp.get(ar1[i])+1);
}
else
{
mp.put(ar1[i], 1);
}
}
// Traverse second array and reduce frequencies
// of common elements.
for (int i = 0; i < n; i++)
if (mp.containsKey(ar2[i]))
mp.put(ar2[i], mp.get(ar2[i])-1);
// Now traverse first array again and print
// all those elements whose frequencies are
// more than 0. To avoid repeated printing,
// we set frequency as 0 after printing.
for (int i = 0; i < m; i++)
{
if (mp.get(ar1[i]) > 0)
{
System.out.print(ar1[i] + " ");
mp.put(ar1[i], 0);
}
}
}
// Driver code
public static void main(String[] args)
{
int ar1[] = { 1, 2, 2, 2, 3, 3, 4, 5 };
int ar2[] = { 2, 2, 3, 3, 3, 4 };
int m = ar1.length;
int n = ar2.length;
moreFreq(ar1, ar2, m, n);
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 program to print all those elements of
# first array that have more frequencies than
# second array.
import math as mt
# Compares two intervals according to
# staring times.
def moreFreq(ar1, ar2, m, n):
# Traverse first array and store
# frequencies of all elements
mp = dict()
for i in range(m):
if ar1[i] in mp.keys():
mp[ar1[i]] += 1
else:
mp[ar1[i]] = 1
# Traverse second array and reduce
# frequencies of common elements.
for i in range(n):
if ar2[i] in mp.keys():
mp[ar2[i]] -= 1
# Now traverse first array again and print
# all those elements whose frequencies are
# more than 0. To avoid repeated printing,
# we set frequency as 0 after printing.
for i in range(m):
if (mp[ar1[i]] > 0):
print(ar1[i], end = " ")
mp[ar1[i]] = 0
# Driver code
ar1 = [ 1, 2, 2, 2, 3, 3, 4, 5 ]
ar2 = [ 2, 2, 3, 3, 3, 4 ]
m = len(ar1)
n = len(ar2)
moreFreq(ar1, ar2, m, n)
# This code is contributed
# by mohit kumar 29
C#
// C# pprogram to print all those elements of
// first array that have more frequencies than
// second array.
using System;
using System.Collections.Generic;
class GFG
{
// Compares two intervals according to
// staring times.
static void moreFreq(int []ar1, int []ar2,
int m, int n)
{
// Traverse first array and store frequencies
// of all elements
Dictionary mp = new Dictionary();
for (int i = 0 ; i < m; i++)
{
if(mp.ContainsKey(ar1[i]))
{
mp[ar1[i]] = mp[ar1[i]] + 1;
}
else
{
mp.Add(ar1[i], 1);
}
}
// Traverse second array and reduce frequencies
// of common elements.
for (int i = 0; i < n; i++)
if (mp.ContainsKey(ar2[i]))
mp[ar2[i]] = mp[ar2[i]] - 1;
// Now traverse first array again and print
// all those elements whose frequencies are
// more than 0. To avoid repeated printing,
// we set frequency as 0 after printing.
for (int i = 0; i < m; i++)
{
if (mp[ar1[i]] > 0)
{
Console.Write(ar1[i] + " ");
mp[ar1[i]] = 0;
}
}
}
// Driver code
public static void Main(String[] args)
{
int []ar1 = { 1, 2, 2, 2, 3, 3, 4, 5 };
int []ar2 = { 2, 2, 3, 3, 3, 4 };
int m = ar1.Length;
int n = ar2.Length;
moreFreq(ar1, ar2, m, n);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
1 2 5
时间复杂度: O(m + n) 假设 unordered_map find() 和 insert() 在 O(1) 时间内工作。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。