Given an array of n elements such that elements may repeat. We can delete any number of elements from the array. The task is to find a minimum number of elements to be deleted from the array to make it equal.**Examples:**

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.

In this problem, we need to minimize the delete operations. The approach is simple, we count the frequency of each element in an array, then find the frequency of the most frequent elements in **count array**. Let this frequency be max_freq. To get the minimum number of elements to be deleted from the array calculate **n – max_freq** where n is a number of elements in the given array.

## C++

`// C++ program to find minimum` `// number of deletes required` `// to make all elements same.` `#include <bits/stdc++.h>` `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<` `int` `, ` `int` `> 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;` `}` |

*chevron_right*

*filter_none*

## 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<Integer, Integer> 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<Integer, ` ` ` `Integer> 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` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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<` `int` `, ` `int` `> freq` ` ` `= ` `new` `Dictionary<` `int` `, ` `int` `>();` ` ` `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<` `int` `, ` `int` `> 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` |

*chevron_right*

*filter_none*

**Output:**

2

**Time complexity:** O(n)

** Note:** Here we can optimize the extra space to count the frequency of each element to O(1) but for this, we have to modify our original array. See this article.

This article is contributed by **Shashank Mishra ( Gullu )**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number of operations required to delete all elements of the array
- Minimum increments to make all array elements equal with sum same as the given array after exactly one removal
- Minimum operations to make counts of remainders same in an array
- Minimum number of increment-other operations to make all array elements equal.
- Minimum gcd operations to make all array elements one
- Minimum operations required to make all Array elements divisible by K
- Minimum no. of operations required to make all Array Elements Zero
- Minimum operations required to make all the array elements equal
- Minimum number of operations on an array to make all elements 0
- Find the minimum number of operations required to make all array elements equal
- Make all the array elements odd with minimum operations of given type
- Minimum operations to make all elements equal using the second array
- Minimum Cost to make all array elements equal using given operations
- Minimum increment by k operations to make all elements equal
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Minimum replacements to make elements of a ternary array same
- Minimum Group Flips to Make Binary Array Elements Same