# Elements of first array that have more frequencies

Given two arrays (which may or may not be sorted). There 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.

## 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 staring 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; ` `} ` |

*chevron_right*

*filter_none*

## 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<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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to prall 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 ` |

*chevron_right*

*filter_none*

**Output:**

1 2 5

**Time Complexity :** O(m + n) under the assumption that unordered_map find() and insert() work in O(1) time.

## Recommended Posts:

- Counting frequencies of array elements
- Array elements with prime frequencies
- Range Queries for Frequencies of array elements
- Find array elements with frequencies in range [l , r]
- Sum of elements in an array with frequencies greater than or equal to that element
- Count frequencies of all elements in array in Python using collections module
- Count frequencies of all elements in array in O(1) extra space and O(n) time
- Difference between highest and least frequencies in an array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find original array from encrypted array (An array of sums of other elements)
- Average of remaining elements after removing K largest and K smallest elements from array
- Find elements larger than half of the elements in an array
- Elements to be added so that all elements of a range are present in array
- Find all elements in array which have at-least two greater elements
- Count array elements that divide the sum of all other elements

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.