📌  相关文章
📜  用其在数组中的频率替换数组中的每个元素

📅  最后修改于: 2021-10-27 16:56:48             🧑  作者: Mango

给定一个整数数组,用它在数组中的频率替换每个元素。

例子:

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

方法:

  1. 取一个哈希映射,它将存储数组中所有元素的频率。
  2. 现在,再次穿越。
  3. 现在,用它的频率替换所有元素。
  4. 打印修改后的数组。
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 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 mp = new HashMap();
 
        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 mp = new Dictionary();
 
        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


输出 :

1 3 2 3 3 2

时间复杂度– O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程