# Minimum delete operations to make all elements of array same

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

## 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<``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); ` `    ``cout << minDelete(arr, n); ` `    ``return` `0; ` `} `

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

Output:

```2
```

Time complexity: O(n)

Note : Here we can optimize the extra space to count 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : grand_master

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.