Remove elements from the array which appear more than k times - GeeksforGeeks

01 Jun, 2021
Given an array of integers, remove all the occurrences of those elements which appear strictly more than k times in the array.Examples: 
Input : arr[] = {1, 2, 2, 3, 2, 3, 4}
        k = 2
Output : 1 3 3 4

Input : arr[] = {2, 5, 5, 7}
        k = 1
Output : 2 7
Approach:  
Take a hash map, which will store the frequency of all the elements in the array.
Now, traverse once again.
Print the elements which appear less than or equal to k times.
C++
Java
Python3
C#
Javascript
// C++ program to remove the elements which// appear more than k times from the array.#include "iostream"#include "unordered_map"using namespace std; void RemoveElements(int arr[], int n, int k){    // Hash map which will store the    // frequency of the elements of the array.    unordered_map<int, int> mp;     for (int i = 0; i < n; ++i) {        // Incrementing the frequency        // of the element by 1.        mp[arr[i]]++;    }     for (int i = 0; i < n; ++i) {        // Print the element which appear        // less than or equal to k times.        if (mp[arr[i]] <= k) {            cout << arr[i] << " ";        }    }} int main(int argc, char const* argv[]){    int arr[] = { 1, 2, 2, 3, 2, 3, 4 };    int n = sizeof(arr) / sizeof(arr[0]);     int k = 2;     RemoveElements(arr, n, k);    return 0;}
// Java program to remove the elements which// appear more than k times from the array.import java.util.HashMap;import java.util.Map; class GFG{ static void RemoveElements(int arr[], int n, int k){    // Hash map which will store the    // frequency of the elements of the array.    Map<Integer,Integer> mp = new HashMap<>();     for (int i = 0; i < n; ++i)    {        // Incrementing the frequency        // of the element by 1.        mp.put(arr[i],mp.get(arr[i]) == null?1:mp.get(arr[i])+1);     }     for (int i = 0; i < n; ++i)    {        // Print the element which appear        // less than or equal to k times.        if (mp.containsKey(arr[i]) && mp.get(arr[i]) <= k)        {            System.out.print(arr[i] + " ");        }    }} // Driver codepublic static void main(String[] args){    int arr[] = { 1, 2, 2, 3, 2, 3, 4 };    int n = arr.length;     int k = 2;     RemoveElements(arr, n, k);     }} // This code is contributed by Rajput-Ji
# Python 3 program to remove the elements which# appear more than k times from the array.def RemoveElements(arr, n, k):         # Hash map which will store the    # frequency of the elements of the array.    mp = {i:0 for i in range(len(arr))}     for i in range(n):                 # Incrementing the frequency        # of the element by 1.        mp[arr[i]] += 1     for i in range(n):                 # Print the element which appear        # less than or equal to k times.        if (mp[arr[i]] <= k):            print(arr[i], end = " ") # Driver Code   if __name__ == '__main__':    arr = [1, 2, 2, 3, 2, 3, 4]    n = len(arr)     k = 2     RemoveElements(arr, n, k) # This code is contributed by# Sahil_Shelangia
// C# program to remove the elements which// appear more than k times from the array.using System;using System.Collections.Generic; class GFG{static void RemoveElements(int [] arr,                           int n, int k){    // Hash map which will store the    // frequency of the elements of the array.    Dictionary<int,               int> mp = new Dictionary<int,                                        int>();     for (int i = 0; i < n; ++i)    {        // Incrementing the frequency        // of the element by 1.        if(mp.ContainsKey(arr[i]))            mp[arr[i]]++;        else            mp[arr[i]] = 1;    }     for (int i = 0; i < n; ++i)    {        // Print the element which appear        // less than or equal to k times.        if (mp.ContainsKey(arr[i]) && mp[arr[i]] <= k)        {            Console.Write(arr[i] + " ");        }    }} // Driver codestatic public void Main(){    int [] arr = { 1, 2, 2, 3, 2, 3, 4 };    int n = arr.Length;     int k = 2;     RemoveElements(arr, n, k);}} // This code is contributed by Mohit kumar 29
<script> // JavaScript program to remove the elements which// appear more than k times from the array. function RemoveElements(arr,n,k){    // Hash map which will store the    // frequency of the elements of the array.    let mp = new Map();      for (let i = 0; i < n; ++i)    {        // Incrementing the frequency        // of the element by 1.        mp.set(arr[i],mp.get(arr[i]) == null?1:mp.get(arr[i])+1);      }      for (let i = 0; i < n; ++i)    {        // Print the element which appear        // less than or equal to k times.        if (mp.has(arr[i]) && mp.get(arr[i]) <= k)        {            document.write(arr[i] + " ");        }    }} // Driver codelet arr=[1, 2, 2, 3, 2, 3, 4 ];let n = arr.length;let k = 2;RemoveElements(arr, n, k);      // This code is contributed by unknown2108 </script>
Output: 
1 3 3 4
Time Complexity – O(N)
Count the frequencies of every element using Counter function
Traverse the array.
Print the elements which appear less than or equal to k times.
Below is the implementation of the above approach:
Python3
# Python3 program to remove the elements which# appear strictly less than k times from the array.from collections import Counter def removeElements(arr, n, k):     # Calculating frequencies    # using Counter function    freq = Counter(arr)    for i in range(n):         # Print the element which appear        # more than or equal to k times.        if (freq[arr[i]] <= k):            print(arr[i], end=" ")  # Driver Codearr = [1, 2, 2, 3, 2, 3, 4]n = len(arr)k = 2removeElements(arr, n, k) # This code is contributed by vikkycirus
Output:
1 3 3 4 
sahilshelangia
Rajput-Ji
mohit kumar 29
vikkycirus
unknown2108
cpp-unordered_map
frequency-counting
Technical Scripter 2018
Arrays
Competitive Programming
Hash
Searching
Technical Scripter
Arrays
Searching
Hash
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Prefix Sum Array - Implementation and Applications in Competitive Programming
Fast I/O for Competitive Programming