📌  相关文章
📜  使数组的所有元素相同的最少删除操作

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

给定一个包含 n 个元素的数组,使得元素可以重复。我们可以从数组中删除任意数量的元素。任务是找到要从数组中删除的最少元素数以使其相等。
例子:

Input: arr[] = {4, 3, 4, 4, 2, 4}
Output: 2
After deleting 2 and 3 from array, array becomes 
arr[] = {4, 4, 4, 4} 

Input: arr[] = {1, 2, 3, 4, 5}
Output: 4
We can delete any four elements from array.

在这个问题中,我们需要尽量减少删除操作。方法很简单,我们统计一个数组中每个元素出现的频率,然后找到count数组中出现频率最高的元素。将此频率设为 max_freq。要获得要从数组中删除的最小元素数,请计算n – max_freq ,其中 n 是给定数组中的元素数。

C++
// C++ program to find minimum
// number of deletes required
// to make all elements same.
#include 
using namespace std;
 
// Function to get minimum number of elements to be deleted
// from array to make array elements equal
int minDelete(int arr[], int n)
{
    // Create an hash map and store frequencies of all
    // array elements in it using element as key and
    // frequency as value
    unordered_map freq;
    for (int i = 0; i < n; i++)
        freq[arr[i]]++;
 
    // Find maximum frequency among all frequencies.
    int max_freq = INT_MIN;
    for (auto itr = freq.begin(); itr != freq.end(); itr++)
        max_freq = max(max_freq, itr->second);
 
    // To minimize delete operations, we remove all
    // elements but the most frequent element.
    return n - max_freq;
}
 
// Driver program to run the case
int main()
{
    int arr[] = { 4, 3, 4, 4, 2, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minDelete(arr, n);
    return 0;
}


Java
// Java program to find minimum number
// of deletes required to make all
// elements same.
import java.util.*;
 
class GFG{
 
// Function to get minimum number of
// elements to be deleted from array
// to make array elements equal
static int minDelete(int arr[], int n)
{
     
    // Create an hash map and store
    // frequencies of all array elements
    // in it using element as key and
    // frequency as value
    HashMap freq = new HashMap<>();
    for(int i = 0; i < n; i++)
        freq.put(arr[i], freq.getOrDefault(arr[i], 0) + 1);
 
    // Find maximum frequency among all frequencies.
    int max_freq = Integer.MIN_VALUE;
    for(Map.Entry entry : freq.entrySet())
        max_freq = Math.max(max_freq,
                            entry.getValue());
 
    // To minimize delete operations,
    // we remove all elements but the
    // most frequent element.
    return n - max_freq ;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 3, 4, 4, 2, 4 };
    int n = arr.length;
     
    System.out.print(minDelete(arr, n));
}
}
 
// This code is contributed by amal kumar choubey and corrected by Leela Kotte


Python3
# Python3 program to find minimum
# number of deletes required to
# make all elements same.
 
# Function to get minimum number
# of elements to be deleted from
# array to make array elements equal
def minDelete(arr, n):
     
    # Create an dictionary and store
    # frequencies of all array
    # elements in it using
    # element as key and
    # frequency as value
    freq = {}
    for i in range(n):
        if arr[i] in freq:
            freq[arr[i]] += 1
        else:
            freq[arr[i]] = 1;
         
 
    # Find maximum frequency
    # among all frequencies.
    max_freq = 0;
    for i, j in freq.items():
        max_freq = max(max_freq, j);
 
    # To minimize delete operations,
    # we remove all elements but the
    # most frequent element.
    return n - max_freq;
     
# Driver code
arr = [ 4, 3, 4, 4, 2, 4 ];
n = len(arr)
 
print(minDelete(arr, n));
 
# This code is contributed by grand_master


C#
// C# program to find minimum number
// of deletes required to make all
// elements same.
using System;
using System.Collections.Generic;
class GFG {
 
    // Function to get minimum number of
    // elements to be deleted from array
    // to make array elements equal
    static int minDelete(int[] arr, int n)
    {
 
        // Create an hash map and store
        // frequencies of all array elements
        // in it using element as key and
        // frequency as value
        Dictionary freq
            = new Dictionary();
        for (int i = 0; i < n; i++)
            if (freq.ContainsKey(arr[i]))
            {
                freq[arr[i]] = freq[arr[i]] + 1;
            }
            else
            {
                freq.Add(arr[i], 1);
            }
 
        // Find maximum frequency among all frequencies.
        int max_freq = int.MinValue;
        foreach(KeyValuePair entry in freq)
            max_freq = Math.Max(max_freq, entry.Value);
 
        // To minimize delete operations,
        // we remove all elements but the
        // most frequent element.
        return n - max_freq + 1;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = {4, 3, 4, 4, 2, 4};
        int n = arr.Length;
          Console.Write(minDelete(arr, n));
    }
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:

2

时间复杂度: O(n)

注意:这里我们可以优化额外的空间以将每个元素的频率计算为 O(1),但为此,我们必须修改原始数组。看到这篇文章。

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