Given an array of integers arr[] of size N, the task is to find the maximum element in the array whose frequecny equals to it’s value
Examples:
Input: arr[] = {3, 2, 2, 3, 4, 3}
Output: 3
Frequency of element 2 is 2
Frequency of element 3 is 3
Frequency of element 4 is 1
2 and 3 are elements which have same frequency as it’s value and 3 is the maximum.Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 1
Approach: Store the frequency of every element of the array using the map, and finally find out the maximum of those element whose frequency is equal to their value.
Below is the implementation of the above approach:
CPP
// C++ program to find the maximum element // whose frequency equals to it’s value #include <bits/stdc++.h> using namespace std; // Function to find the maximum element // whose frequency equals to it’s value int find_maxm(int arr[], int n) { // Hash map for counting frquency map<int, int> mpp; for (int i = 0; i < n; i++) { // Counting freq of each element mpp[arr[i]] += 1; } int ans = 0; for (auto x : mpp) { int value = x.first; int freq = x.second; // Check if value equls to frequency // and it is the maximum element or not if (value == freq) { ans = max(ans, value); } } return ans; } // Driver code int main() { int arr[] = { 3, 2, 2, 3, 4, 3 }; // Size of array int n = sizeof(arr) / sizeof(arr[0]); // Function call cout << find_maxm(arr, n); return 0; } |
Java
// Java program to find the maximum element // whose frequency equals to it’s value import java.util.*; class GFG{ // Function to find the maximum element // whose frequency equals to it’s value static int find_maxm(int arr[], int n) { // Hash map for counting frquency HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>(); for (int i = 0; i < n; i++) { // Counting freq of each element if(mp.containsKey(arr[i])){ mp.put(arr[i], mp.get(arr[i])+1); }else{ mp.put(arr[i], 1); } } int ans = 0; for (Map.Entry<Integer,Integer> x : mp.entrySet()) { int value = x.getKey(); int freq = x.getValue(); // Check if value equls to frequency // and it is the maximum element or not if (value == freq) { ans = Math.max(ans, value); } } return ans; } // Driver code public static void main(String[] args) { int arr[] = { 3, 2, 2, 3, 4, 3 }; // Size of array int n = arr.length; // Function call System.out.print(find_maxm(arr, n)); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program to find the maximum element # whose frequency equals to it’s value # Function to find the maximum element # whose frequency equals to it’s value def find_maxm(arr, n) : # Hash map for counting frquency mpp = {} for i in range(0,n): # Counting freq of each element if(arr[i] in mpp): mpp.update( {arr[i] : mpp[arr[i]] + 1} ) else: mpp[arr[i]] = 1 ans = 0 for value,freq in mpp.items(): # Check if value equls to frequency # and it is the maximum element or not if (value == freq): ans = max(ans, value) return ans # Driver code arr = [ 3, 2, 2, 3, 4, 3 ] # Size of array n = len(arr) # Function call print(find_maxm(arr, n)) # This code is contributed by Sanjit_Prasad |
C#
// C# program to find the maximum element // whose frequency equals to it’s value using System; using System.Collections.Generic; class GFG{ // Function to find the maximum element // whose frequency equals to it’s value static int find_maxm(int []arr, int n) { // Hash map for counting frquency Dictionary<int,int> mp = new Dictionary<int,int>(); for (int i = 0; i < n; i++) { // Counting freq of each element if(mp.ContainsKey(arr[i])){ mp[arr[i]] = mp[arr[i]]+1; }else{ mp.Add(arr[i], 1); } } int ans = 0; foreach (KeyValuePair<int,int> x in mp) { int value = x.Key; int freq = x.Value; // Check if value equls to frequency // and it is the maximum element or not if (value == freq) { ans = Math.Max(ans, value); } } return ans; } // Driver code public static void Main(String[] args) { int []arr = { 3, 2, 2, 3, 4, 3 }; // Size of array int n = arr.Length; // Function call Console.Write(find_maxm(arr, n)); } } // This code is contributed by Rajput-Ji |
3
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:
- Find element in array with frequency equal to sum of frequencies of other elements
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Replace each element by the difference of the total size of the array and frequency of that element
- Design a stack which can give maximum frequency element
- Check if frequency of each element in given array is unique or not
- Find the element having different frequency than other array elements
- Cumulative frequency of count of each element in an unsorted array
- Sorting element of an array by frequency in decreasing order
- Frequency of each element of an array of small ranged values
- Maximum element in an array such that its previous and next element product is maximum
- Find frequency of each element in a limited range array in less than O(n) time
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Find an element in array such that sum of left array is equal to sum of right array
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Maximum possible remainder when an element is divided by other element in the array
- Sum of (maximum element - minimum element) for all the subsets of an array.
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- For each element in 1st array count elements less than or equal to it in 2nd array
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.

