Reemplace todos los elementos en la array por su frecuencia en la array

Dada una array de enteros, reemplace cada elemento por su frecuencia en la array.

Ejemplos: 

Input : arr[] = { 1, 2, 5, 2, 2, 5 }
Output : 1 3 2 3 3 2

Input : arr[] = { 4 5 4 5 6 6 6 }
Output : 2 2 2 2 3 3 3

Acercarse:  

  1. Tome un mapa hash, que almacenará la frecuencia de todos los elementos en la array.
  2. Ahora, atraviesa una vez más.
  3. Ahora, reemplaza todos los elementos por su frecuencia.
  4. Imprime la array modificada.

C++

// C++ program to replace the elements
// by their frequency in the array.
#include "iostream"
#include "unordered_map"
using namespace std;
 
void ReplaceElementsByFrequency(int arr[], int n)
{
    // Hash map which will store the
    // frequency of the elements of the array.
    unordered_map<int, int> mp;
 
    for (int i = 0; i < n; ++i) {
 
        // Increment the frequency
        // of the element by 1.
        mp[arr[i]]++;
    }
 
    // Replace every element by its frequency
    for (int i = 0; i < n; ++i) {
        arr[i] = mp[arr[i]];
    }
}
 
int main()
{
    int arr[] = { 1, 2, 5, 2, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    ReplaceElementsByFrequency(arr, n);
 
    // Print the modified array.
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
    return 0;
}

Java

import java.util.HashMap;
 
// Java program to replace the elements
// by their frequency in the array.
class GFG {
 
    static void ReplaceElementsByFrequency(int arr[], int n) {
        // Hash map which will store the
        // frequency of the elements of the array.
        HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>();
 
        for (int i = 0; i < n; ++i) {
 
            // Increment the frequency
            // of the element by 1.
            if (mp.get(arr[i]) == null) {
                mp.put(arr[i], 1);
            } else {
                mp.put(arr[i], (mp.get(arr[i]) + 1));
            }
            //mp[arr[i]]++;
        }
 
        // Replace every element by its frequency
        for (int i = 0; i < n; ++i) {
            if (mp.get(arr[i]) != null) {
                arr[i] = mp.get(arr[i]);
            }
            //arr[i] = mp[arr[i]];
        }
    }
 
    public static void main(String[] args) {
        int arr[] = {1, 2, 5, 2, 2, 5};
        int n = arr.length;
 
        ReplaceElementsByFrequency(arr, n);
 
        // Print the modified array.
        for (int i = 0; i < n; ++i) {
            System.out.print(arr[i] + " ");
        }
    }
}
// This code contributed by 29AJayKumar

Python3

# Python 3 program to replace the elements
# by their frequency in the array.
 
def ReplaceElementsByFrequency(arr, n):
     
    # Hash map which will store the
    # frequency of the elements of the array.
    mp = {i:0 for i in range(len(arr))}
 
    for i in range(n):
         
        # Increment the frequency of the
        # element by 1.
        mp[arr[i]] += 1
 
    # Replace every element by its frequency
    for i in range(n):
        arr[i] = mp[arr[i]]
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 5, 2, 2, 5]
    n = len(arr)
 
    ReplaceElementsByFrequency(arr, n);
 
    # Print the modified array.
    for i in range(n):
        print(arr[i], end = " ")
 
# This code is contributed by
# Sahil_shelangia

C#

// C# program to replace the elements
// by their frequency in the array.
using System;
using System.Collections.Generic;
     
class GFG
{
 
    static void ReplaceElementsByFrequency(int []arr, int n)
    {
        // Hash map which will store the
        // frequency of the elements of the array.
        Dictionary<int,int> mp = new Dictionary<int,int>();
 
        for (int i = 0; i < n; ++i)
        {
 
            // Increment the frequency
            // of the element by 1.
            if (!mp.ContainsKey(arr[i]))
            {
                mp.Add(arr[i], 1);
            }
            else
            {
                var a = mp[arr[i]] + 1;
                mp.Remove(arr[i]);
                mp.Add(arr[i], a);
            }
        }
 
        // Replace every element by its frequency
        for (int i = 0; i < n; ++i)
        {
            if (mp[arr[i]] != 0)
            {
                arr[i] = mp[arr[i]];
            }
            //arr[i] = mp[arr[i]];
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = {1, 2, 5, 2, 2, 5};
        int n = arr.Length;
 
        ReplaceElementsByFrequency(arr, n);
 
        // Print the modified array.
        for (int i = 0; i < n; ++i)
        {
            Console.Write(arr[i] + " ");
        }
    }
}
 
// This code contributed by Rajput-Ji

Javascript

<script>
 
// JavaScript program to replace the elements
// by their frequency in the array.
function ReplaceElementsByFrequency(arr, n)
{
     
    // Hash map which will store the
    // frequency of the elements of the array.
    let mp = new Map();
 
    for(let i = 0; i < n; ++i)
    {
         
        // Increment the frequency
        // of the element by 1.
        if (mp.get(arr[i]) == null)
        {
            mp.set(arr[i], 1);
        }
        else
        {
            mp.set(arr[i], (mp.get(arr[i]) + 1));
        }
        //mp[arr[i]]++;
    }
 
    // Replace every element by its frequency
    for(let i = 0; i < n; ++i)
    {
        if (mp.get(arr[i]) != null)
        {
            arr[i] = mp.get(arr[i]);
        }
        //arr[i] = mp[arr[i]];
    }
}
 
// Driver Code
let arr = [ 1, 2, 5, 2, 2, 5 ];
let n = arr.length;
 
ReplaceElementsByFrequency(arr, n);
 
// Print the modified array.
for(let i = 0; i < n; ++i)
{
    document.write(arr[i] + " ");
}
 
// This code is contributed by code_hunt
 
</script>

Producción : 

1 3 2 3 3 2

Complejidad del tiempo – O(N)
 

Publicación traducida automáticamente

Artículo escrito por imdhruvgupta 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 *