Minimum delete operations to make all elements of array same
Last Updated :
11 Sep, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int minDelete( int arr[], int n)
{
unordered_map< int , int > freq;
for ( int i = 0; i < n; i++)
freq[arr[i]]++;
int max_freq = INT_MIN;
for ( auto itr = freq.begin(); itr != freq.end(); itr++)
max_freq = max(max_freq, itr->second);
return n - max_freq;
}
int main()
{
int arr[] = { 4, 3, 4, 4, 2, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minDelete(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minDelete( int arr[], int n)
{
HashMap<Integer, Integer> freq = new HashMap<>();
for ( int i = 0 ; i < n; i++)
freq.put(arr[i], freq.getOrDefault(arr[i], 0 ) + 1 );
int max_freq = Integer.MIN_VALUE;
for (Map.Entry<Integer,
Integer> entry : freq.entrySet())
max_freq = Math.max(max_freq,
entry.getValue());
return n - max_freq ;
}
public static void main(String[] args)
{
int arr[] = { 4 , 3 , 4 , 4 , 2 , 4 };
int n = arr.length;
System.out.print(minDelete(arr, n));
}
}
|
Python3
def minDelete(arr, n):
freq = {}
for i in range (n):
if arr[i] in freq:
freq[arr[i]] + = 1
else :
freq[arr[i]] = 1 ;
max_freq = 0 ;
for i, j in freq.items():
max_freq = max (max_freq, j);
return n - max_freq;
arr = [ 4 , 3 , 4 , 4 , 2 , 4 ];
n = len (arr)
print (minDelete(arr, n));
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int minDelete( int [] arr, int n)
{
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);
}
int max_freq = int .MinValue;
foreach (KeyValuePair< int , int > entry in freq)
max_freq = Math.Max(max_freq, entry.Value);
return n - max_freq + 1;
}
public static void Main(String[] args)
{
int [] arr = {4, 3, 4, 4, 2, 4};
int n = arr.Length;
Console.Write(minDelete(arr, n));
}
}
|
Javascript
<script>
function minDelete(arr, n)
{
let freq = new Map();
for (let i = 0; i < n; i++){
if (freq.has(arr[i])){
freq.set(arr[i], freq.get(arr[i]) + 1)
} else {
freq.set(arr[i], 1)
}
}
let max_freq = Number.MIN_SAFE_INTEGER;
for (let entry of freq)
max_freq = Math.max(max_freq, entry[1]);
return n - max_freq ;
}
let arr = [4, 3, 4, 4, 2, 4 ];
let n = arr.length;
document.write(minDelete(arr, n));
</script>
|
Output:
2
Time complexity: O(n)
Auxiliary Space: O(n)
Another Approach Using binary search :
- .First , we will sort the array for binary search function . Then we can find frequency of all array elements using lower_bound and upper bound .
- Then our answer will be n – max_frequency .
- Then return final answer
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minDelete( int arr[], int n)
{
int max_freq = 1;
sort(arr,arr+n);
for ( int i = 0 ; i < n ;i++)
{
int first_index = lower_bound(arr,arr+n,arr[i])- arr;
int last_index = upper_bound(arr,arr+n,arr[i])- arr-1;
i = last_index;
int fre = last_index-first_index+1;
max_freq = max( max_freq , fre );
}
return n-max_freq;
}
int main()
{
int arr[] = { 4, 3, 4, 4, 2, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minDelete(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static int minDelete( int [] arr) {
int n = arr.length;
int max_freq = 1 ;
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++) {
int first_index = Arrays.binarySearch(arr, arr[i]);
int last_index = first_index;
while (last_index < n - 1 && arr[last_index + 1 ] == arr[i]) {
last_index++;
}
i = last_index;
int fre = last_index - first_index + 1 ;
max_freq = Math.max(max_freq, fre);
}
return n - max_freq;
}
public static void main(String[] args) {
int [] arr = { 4 , 3 , 4 , 4 , 2 , 4 };
System.out.println(minDelete(arr));
}
}
|
Python3
def minDelete(arr, n):
max_freq = 1
arr.sort()
i = 0
while i < n:
first_index = arr.index(arr[i])
j = n - 1
while j > = 0 and arr[j] ! = arr[i]:
j - = 1
last_index = j
fre = last_index - first_index + 1
max_freq = max (max_freq, fre)
i = last_index + 1
return n - max_freq
arr = [ 4 , 3 , 4 , 4 , 2 , 4 ]
n = len (arr)
print (minDelete(arr, n))
|
C#
using System;
class Program {
static int minDelete( int [] arr, int n)
{
int max_freq = 1;
Array.Sort(arr);
for ( int i = 0; i < n; i++) {
int first_index
= Array.BinarySearch(arr, arr[i]);
int last_index = Array.LastIndexOf(arr, arr[i]);
i = last_index;
int fre = last_index - first_index
+ 1;
max_freq = Math.Max(max_freq, fre);
}
return n - max_freq;
}
static void Main( string [] args)
{
int [] arr = { 4, 3, 4, 4, 2, 4 };
int n = arr.Length;
Console.WriteLine(minDelete(arr, n));
}
}
|
Javascript
function minDelete(arr, n) {
let max_freq = 1;
arr.sort((a, b) => a - b);
for (let i = 0; i < n; i++) {
let first_index = arr.indexOf(arr[i]);
let last_index = arr.lastIndexOf(arr[i]);
i = last_index;
let fre = last_index - first_index + 1;
max_freq = Math.max(max_freq, fre);
}
return n - max_freq;
}
let arr = [4, 3, 4, 4, 2, 4];
let n = arr.length;
console.log(minDelete(arr, n));
|
Time Complexity: O(N*Log2N), where N is the size of the input array
Auxiliary Space: O(1)
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.
Share your thoughts in the comments
Please Login to comment...