Elements of first array that have more frequencies
Last Updated :
29 Aug, 2022
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 : 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
The idea is to use hashing. We traverse first array and insert all elements and their frequencies in a hash table. Now we traverse through the second array and reduce frequencies in hash table for the common elements. Now we traverse through first array again and print those elements whose frequencies are still more than 0. To avoid repeated printing of same elements, we set frequency as 0.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void moreFreq( int ar1[], int ar2[], int m, int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < m; i++)
mp[ar1[i]]++;
for ( int i = 0; i < n; i++)
if (mp.find(ar2[i]) != mp.end())
mp[ar2[i]]--;
for ( int i = 0; i < m; i++) {
if (mp[ar1[i]] > 0) {
cout << ar1[i] << " " ;
mp[ar1[i]] = 0;
}
}
}
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
import java.util.*;
class GFG
{
static void moreFreq( int ar1[], int ar2[], int m, int n)
{
Map<Integer,Integer> 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 );
}
}
for ( int i = 0 ; i < n; i++)
if (mp.containsKey(ar2[i]))
mp.put(ar2[i], mp.get(ar2[i])- 1 );
for ( int i = 0 ; i < m; i++)
{
if (mp.get(ar1[i]) > 0 )
{
System.out.print(ar1[i] + " " );
mp.put(ar1[i], 0 );
}
}
}
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);
}
}
|
Python3
import math as mt
def moreFreq(ar1, ar2, m, n):
mp = dict ()
for i in range (m):
if ar1[i] in mp.keys():
mp[ar1[i]] + = 1
else :
mp[ar1[i]] = 1
for i in range (n):
if ar2[i] in mp.keys():
mp[ar2[i]] - = 1
for i in range (m):
if (mp[ar1[i]] > 0 ):
print (ar1[i], end = " " )
mp[ar1[i]] = 0
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)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void moreFreq( int []ar1, int []ar2,
int m, int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
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);
}
}
for ( int i = 0; i < n; i++)
if (mp.ContainsKey(ar2[i]))
mp[ar2[i]] = mp[ar2[i]] - 1;
for ( int i = 0; i < m; i++)
{
if (mp[ar1[i]] > 0)
{
Console.Write(ar1[i] + " " );
mp[ar1[i]] = 0;
}
}
}
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);
}
}
|
Javascript
<script>
function moreFreq(ar1, ar2, m, n)
{
var mp = new Map();
for ( var i = 0; i < m; i++)
{
if (mp.has(ar1[i]))
mp.set(ar1[i], mp.get(ar1[i])+1)
else
mp.set(ar1[i], 1)
}
for ( var i = 0; i < n; i++)
if (mp.has(ar2[i]))
{
mp.set(ar2[i], mp.get(ar2[i])-1)
}
for ( var i = 0; i < m; i++) {
if (mp.get(ar1[i]) > 0) {
document.write( ar1[i] + " " );
mp.set(ar1[i], 0);
}
}
}
var ar1 = [1, 2, 2, 2, 3, 3, 4, 5];
var ar2 = [2, 2, 3, 3, 3, 4];
var m = ar1.length;
var n = ar2.length;
moreFreq(ar1, ar2, m, n);
</script>
|
Time Complexity : O(m + n) under the assumption that unordered_map find() and insert() work in O(1) time.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...