Given two arrays (which may or may not be sorted) of sizes M and N respectively. There arrays are such that they might have some common elements in them. You need to count the number of elements whose occurrences are more in first array than second.
Examples:
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 4Input : 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
The idea is to use a hash map and keep the count of frequency of numbers in the first array. While traversing the second array reduce the count in the map for every integer encountered. Now count the elements whose frequency is more than 0 otherwise return 0.
C++
// C++ to count number of elements present in arr1 whose // occurrence is more than in arr2 #include <bits/stdc++.h> 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<int, int> mp; // frequency of elements of arr1 is calulated 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<Integer, Integer> mp = new HashMap<Integer, Integer>(); // frequency of elements of arr1 is calulated 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 calulated 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<int, int> mp = new Dictionary<int, int>(); // frequency of elements // of arr1 is calulated 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 |
4
Time complexity: O(m + n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Find elements which are present in first array and not in second
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count distinct elements after adding each element of First Array with Second Array
- Find the character in first string that is present at minimum index in second string
- Count of strings in the first array which are smaller than every string in the second array
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Check if all array elements are present in a given stack or not
- Find the first, second and third minimum elements in an array
- Find the sum of the first half and second half elements of an array
- Find the maximum elements in the first and the second halves of the Array
- Find the XOR of first half and second half elements of an array
- First string from the given array whose reverse is also present in the same array
- Count items common to both the lists but with different prices
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Find just strictly greater element from first array for each element in second array
- Elements to be added so that all elements of a range are present in array
- Count of elements A[i] such that A[i] + 1 is also present in the Array
- Count of times second string can be formed from the characters of first string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

