Reemplace cada elemento por la diferencia del tamaño total de la array y la frecuencia de ese elemento

Dada una array de enteros, la tarea es reemplazar cada elemento por la diferencia del tamaño total de la array y su frecuencia.

Ejemplos: 

Input: arr[] = { 1, 2, 5, 2, 2, 5, 4 }
Output: 6 4 5 4 4 5 6
Size of the array is 7.
The frequency of 1 is 1. So replace it by 7-1 = 6
The frequency of 2 is 3. So replace it by 7-3 = 4

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

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, reemplace todos los elementos por la diferencia del tamaño total de la array y su frecuencia.
  4. Imprime la array modificada.

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ program to Replace each element
// by the difference of the total size
// of the array and its frequency
#include <bits/stdc++.h>
using namespace std;
 
// Function to replace the elements
void ReplaceElements(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] = n - mp[arr[i]];
 
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 5, 2, 2, 5, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    ReplaceElements(arr, n);
 
    // Print the modified array.
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
 
    return 0;
}

Java

// Java program to Replace each element
// by the difference of the total size
// of the array and its frequency
import java.util.*;
 
class GFG
{
 
    // Function to replace the elements
    static void ReplaceElements(int arr[], int n)
    {
        // Hash map which will store the
        // frequency of the elements of the array.
        HashMap<Integer, Integer> mp = new HashMap<>();
 
        for (int i = 0; i < n; i++)
        {
 
            // Increment the frequency
            // of the element by 1.
            if (!mp.containsKey(arr[i]))
            {
                mp.put(arr[i], 1);
            }
            else
            {
                mp.put(arr[i], mp.get(arr[i]) + 1);
            }
        }
 
        // Replace every element by its frequency
        for (int i = 0; i < n; ++i)
        {
            arr[i] = n - mp.get(arr[i]);
        }
 
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {1, 2, 5, 2, 2, 5, 4};
        int n = arr.length;
 
        ReplaceElements(arr, n);
 
        // Print the modified array.
        for (int i = 0; i < n; ++i)
        {
            System.out.print(arr[i] + " ");
        }
    }
}
 
// This code contributed by Rajput-Ji

Python3

# Python3 program to Replace each element
# by the difference of the total size
# of the array and its frequency
 
# Function to replace the elements
def ReplaceElements(arr, n):
     
    # Hash map which will store the
    # frequency of the elements of the array.
    mp = dict()
 
    for i in range(n):
         
        # Increment the frequency
        # of the element by 1.
        mp[arr[i]] = mp.get(arr[i], 0) + 1
 
    # Replace every element by its frequency
    for i in range(n):
        arr[i] = n - mp[arr[i]]
 
# Driver code
arr = [1, 2, 5, 2, 2, 5, 4]
n = len(arr)
 
ReplaceElements(arr, n)
 
# Print the modified array.
for i in range(n):
    print(arr[i], end = " ")
 
# This code is contributed by mohit kumar

C#

// C# program to Replace each element
// by the difference of the total size
// of the array and its frequency
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to replace the elements
    static void ReplaceElements(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)
        {
            arr[i] = n - mp[arr[i]];
        }
 
    }
 
    // Driver code
    public static void Main()
    {
        int []arr = {1, 2, 5, 2, 2, 5, 4};
        int n = arr.Length;
 
        ReplaceElements(arr, n);
 
        // Print the modified array.
        for (int i = 0; i < n; ++i)
        {
            Console.Write(arr[i] + " ");
        }
    }
}
 
/* This code contributed by PrinciRaj1992 */

Javascript

<script>
 
// JavaScript program to Replace each element
// by the difference of the total size
// of the array and its frequency
 
// Function to replace the elements
function ReplaceElements(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.has(arr[i]))
        {
            mp.set(arr[i], 1);
        }
        else
        {
            mp.set(arr[i], mp.get(arr[i]) + 1);
        }
    }
 
    // Replace every element by its frequency
    for(let i = 0; i < n; ++i)
    {
        arr[i] = n - mp.get(arr[i]);
    }
}
 
// Driver Code
let arr = [ 1, 2, 5, 2, 2, 5, 4 ];
let n = arr.length;
 
ReplaceElements(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: 

6 4 5 4 4 5 6

 

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 *