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++
// C++ program to print all those elements of
// first array that have more frequencies than
// second array.
#include <bits/stdc++.h>
using namespace std;
// Compares two intervals according to starting times.
void moreFreq(int ar1[], int ar2[], int m, int n)
{
// Traverse first array and store frequencies
// of all elements
unordered_map<int, int> 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 starting times.
static void moreFreq(int ar1[], int ar2[], int m, int n)
{
// Traverse first array and store frequencies
// of all elements
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);
}
}
// 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
# starting 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# program 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
// starting times.
static void moreFreq(int []ar1, int []ar2,
int m, int n)
{
// Traverse first array and store frequencies
// of all elements
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);
}
}
// 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
<script>
// JavaScript program to print all those elements of
// first array that have more frequencies than
// second array.
// Compares two intervals according to starting times.
function moreFreq(ar1, ar2, m, n)
{
// Traverse first array and store frequencies
// of all elements
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)
}
// Traverse second array and reduce frequencies
// of common elements.
for (var i = 0; i < n; i++)
if (mp.has(ar2[i]))
{
mp.set(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 (var i = 0; i < m; i++) {
if (mp.get(ar1[i]) > 0) {
document.write( ar1[i] + " ");
mp.set(ar1[i], 0);
}
}
}
// Driver code
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.
Similar Reads
Mode of frequencies of given array elements Given an array arr[], the task is to find the mode of frequencies of elements of the given array. Examples: Input: arr[] = {6, 10, 3, 10, 8, 3, 6, 4}, N = 8Output: 2Explanation:Here (3, 10 and 6) have frequency 2, while (4 and 8) have frequency 1.Three numbers have frequency 2, while 2 numbers have
6 min read
Find array elements with frequencies in range [l , r] Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai
9 min read
Range Queries for Frequencies of array elements Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; lef
13 min read
Array range queries for elements with frequency same as value Given an array of N numbers, the task is to answer Q queries of the following type: query(start, end) = Number of times a number x occurs exactly x times in a subarray from start to end Examples: Input : arr = {1, 2, 2, 3, 3, 3} Query 1: start = 0, end = 1, Query 2: start = 1, end = 1, Query 3: star
15+ min read
Find the array element having maximum frequency of the digit K Given an array arr[] of size N and an integer K, the task is to find an array element that contains the digit K a maximum number of times. If more than one solutions exist, then print any one of them. Otherwise, print -1. Examples: Input: arr[] = {3, 77, 343, 456}, K = 3 Output: 343 Explanation: Fre
15 min read