Remove elements from the array which appear more than k times
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++
// 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; } |
chevron_right
filter_none
Java
// 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 code public 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 |
chevron_right
filter_none
Python3
# 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 |
chevron_right
filter_none
Output:
1 3 3 4
Time Complexity – O(N)
Recommended Posts:
- Remove elements that appear strictly less than k times
- Sum of all elements repeating 'k' times in an array
- Sum of array elements after every element x is XORed itself x times
- Two odd occurring elements in an array where all other occur even times
- Unique element in an array where all elements occur k times except one
- Given an array of size n and a number k, find all elements that appear more than n/k times
- Count all elements in the array which appears at least K times after their first occurrence
- Remove minimum elements from array so that max <= 2 * min
- Remove minimum elements from the array such that 2*min becomes more than max
- Remove duplicate elements in an Array using STL in C++
- Remove elements to make array sorted
- Remove elements from the array whose frequency lies in the range [l, r]
- Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
- Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
- Remove minimum number of elements such that no common element exist in both array
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.