Elemento máximo en una array que es igual a su frecuencia

Dada una array de enteros arr[] de tamaño N , la tarea es encontrar el elemento máximo en la array cuya frecuencia es igual a su valor 
Ejemplos: 
 

Entrada: arr[] = {3, 2, 2, 3, 4, 3} 
Salida:
La frecuencia del elemento 2 es 2 
La frecuencia del elemento 3 es 3 
La frecuencia del elemento 4 es 1 
2 y 3 son elementos que tienen la misma frecuencia que su valor y 3 es el máximo.
Entrada: arr[] = {1, 2, 3, 4, 5, 6} 
Salida:
 

Enfoque: almacene la frecuencia de cada elemento de la array utilizando el mapa y, finalmente, descubra el máximo de aquellos elementos cuya frecuencia es igual a su valor.
A continuación se muestra la implementación del enfoque anterior:
 

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 frequency
    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 equals 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 frequency
    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 equals 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 frequency
    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 equals 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 frequency
    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 equals 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

Javascript

<script>
 
// Javascript 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
function find_maxm(arr, n)
{
    // Hash map for counting frequency
    var mpp = new Map();
 
    for (var i = 0; i < n; i++)
    {
     
        // Counting freq of each element
        if(mpp.has(arr[i]))
            mpp.set(arr[i], mpp.get(arr[i])+1)
        else
            mpp.set(arr[i], 1)
    }
 
    var ans = 0;
    mpp.forEach((value, key) => {
         
        var value = value;
        var freq = key;
         
        // Check if value equals to frequency
        // and it is the maximum element or not
        if (value == freq) {
            ans = Math.max(ans, value);
        }
    });
 
    return ans;
}
 
// Driver code
var arr = [3, 2, 2, 3, 4, 3 ];
 
// Size of array
var n = arr.length;
 
// Function call
document.write( find_maxm(arr, n));
 
// This code is contributed by famously.
</script>
Producción: 

3

 

Publicación traducida automáticamente

Artículo escrito por mohit kumar 29 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *