Skip to content
Related Articles

Related Articles

Improve Article

Minimum deletions required such that any number X will occur exactly X times

  • Difficulty Level : Medium
  • Last Updated : 13 May, 2021

Given an array arr[] of N integers, the task is to find the minimum deletions required such that frequency of arr[i] is exactly arr[i] in the array for all possible values of i.
Examples: 
 

Input: arr[] = {1, 2, 2, 3, 3} 
Output:
Frequency(1) = 1 
Frequency(2) = 2 
Frequency(3) = 2, frequency can’t be increased 
So, delete every occurrence of 3.
Input: arr[] = {2, 3, 2, 3, 4, 4, 4, 4, 5} 
Output:
 

 

Approach: There are two cases: 
 

  • If frequency of X is greater than or equal o X then we delete extra frequencies of X to get exactly X elements of value X.
  • If frequency of X is less than X then we delete all of the occurrences of X as it is impossible to get extra element of value X.

Below is the implementation of the above approach: 
 



C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the minimum
// deletions required
int MinDeletion(int a[], int n)
{
 
    // To store the frequency of
    // the array elements
    unordered_map<int, int> map;
 
    // Store frequency of each element
    for (int i = 0; i < n; i++)
        map[a[i]]++;
 
    // To store the minimum deletions required
    int ans = 0;
 
    for (auto i : map) {
 
        // Value
        int x = i.first;
 
        // It's frequency
        int frequency = i.second;
 
        // If number less than or equal
        // to it's frequency
        if (x <= frequency) {
 
            // Delete extra occurrences
            ans += (frequency - x);
        }
 
        // Delete every occurrence of x
        else
            ans += frequency;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { 2, 3, 2, 3, 4, 4, 4, 4, 5 };
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << MinDeletion(a, n);
 
    return 0;
}

Java




// Java Implementation of above approach
import java.util.*;
 
class GFG
{
     
// Function to return the minimum
// deletions required
static int MinDeletion(int a[], int n)
{
 
    // To store the frequency of
    // the array elements
    Map<Integer,Integer> mp = new HashMap<>();
 
    // Store frequency of each element
    for (int i = 0 ; i < n; i++)
    {
        if(mp.containsKey(a[i]))
        {
            mp.put(a[i], mp.get(a[i])+1);
        }
        else
        {
            mp.put(a[i], 1);
        }
    }
    // To store the minimum deletions required
    int ans = 0;
 
    for (Map.Entry<Integer,Integer> i : mp.entrySet())
    {
 
        // Value
        int x = i.getKey();
 
        // It's frequency
        int frequency = i.getValue();
 
        // If number less than or equal
        // to it's frequency
        if (x <= frequency)
        {
 
            // Delete extra occurrences
            ans += (frequency - x);
        }
 
        // Delete every occurrence of x
        else
            ans += frequency;
    }
 
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 2, 3, 2, 3, 4, 4, 4, 4, 5 };
    int n = a.length;
 
    System.out.println(MinDeletion(a, n));
}
}
 
// This code is contributed by Rajput-Ji

Python3




# Python3 implementation of the approach
 
# Function to return the minimum
# deletions required
def MinDeletion(a, n) :
 
    # To store the frequency of
    # the array elements
    map = dict.fromkeys(a, 0);
 
    # Store frequency of each element
    for i in range(n) :
        map[a[i]] += 1;
 
    # To store the minimum deletions required
    ans = 0;
 
    for key,value in map.items() :
 
        # Value
        x = key;
 
        # It's frequency
        frequency = value;
 
        # If number less than or equal
        # to it's frequency
        if (x <= frequency) :
 
            # Delete extra occurrences
            ans += (frequency - x);
 
        # Delete every occurrence of x
        else :
            ans += frequency;
 
    return ans;
 
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 2, 3, 2, 3, 4, 4, 4, 4, 5 ];
    n = len(a);
 
    print(MinDeletion(a, n));
 
# This code is contributed by AnkitRai01

C#




// C# Implementation of above approach
using System;
using System.Collections.Generic;
 
class GFG
{
     
// Function to return the minimum
// deletions required
static int MinDeletion(int []a, int n)
{
 
    // To store the frequency of
    // the array elements
    Dictionary<int,
               int> mp = new Dictionary<int,
                                        int>();
 
    // Store frequency of each element
    for (int i = 0 ; i < n; i++)
    {
        if(mp.ContainsKey(a[i]))
        {
            var val = mp[a[i]];
            mp.Remove(a[i]);
            mp.Add(a[i], val + 1);
        }
        else
        {
            mp.Add(a[i], 1);
        }
    }
     
    // To store the minimum deletions required
    int ans = 0;
 
    foreach(KeyValuePair<int, int> i in mp)
    {
 
        // Value
        int x = i.Key;
 
        // It's frequency
        int frequency = i.Value;
 
        // If number less than or equal
        // to it's frequency
        if (x <= frequency)
        {
 
            // Delete extra occurrences
            ans += (frequency - x);
        }
 
        // Delete every occurrence of x
        else
            ans += frequency;
    }
 
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int []a = { 2, 3, 2, 3, 4, 4, 4, 4, 5 };
    int n = a.Length;
 
    Console.WriteLine(MinDeletion(a, n));
}
}
 
// This code is contributed by PrinciRaj1992

Javascript




<script>
// javaScript implementation of the approach
 
// Function to return the minimum
// deletions required
function MinDeletion( a, n){
    // To store the frequency of
    // the array elements
    let map = new Map();
     
    // Store frequency of each element
    for (let i = 0; i < n; i++){
        if(map[a[i]])
            map[a[i]]++;
        else
            map[a[i]] = 1
     }
 
    // To store the minimum deletions required
    let ans = 0;
    for(var m in map){
         
        // Value
        let x = m;
 
        // It's frequency
        let frequency = map[m];
 
        // If number less than or equal
        // to it's frequency
        if (x <= frequency) {
 
            // Delete extra occurrences
            ans += (frequency - x);
        }
 
        // Delete every occurrence of x
        else
            ans += frequency;
    };
 
    return ans;
}
 
// Driver code
let a = [ 2, 3, 2, 3, 4, 4, 4, 4, 5 ];
let n = a.length;
document.write( MinDeletion(a, n));
 
// This code is contributed by rohitsingh07052.
</script>
Output: 
3

 

Time Complexity: O(N)
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :