Given an array of integers, remove all the elements which appear strictly less than k times.
Examples:
Input : arr[] = {1, 2, 2, 3, 2, 3, 4}
k = 2
Output : 2 2 3 2 3
Explanation : {1, 4} appears less than 2 times.
Approach :
- Take a hash map, which will store the frequency of all the elements in the array.
- Now, traverse once again.
- Remove the elements which appears strictly less than k times.
- Else, print it.
C++
// C++ program to remove the elements which // appear strictly less 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 // more than or equal to k times. if (mp[arr[i]] >= k) { cout << arr[i] << " "; } } } int main() { 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 strictly less than k times from the array. import java.util.HashMap; class geeks { public static void removeElements(int[] arr, int n, int k) { // Hash map which will store the // frequency of the elements of the array. HashMap<Integer, Integer> mp = new HashMap<>(); for (int i = 0; i < n; ++i) { // Incrementing the frequency // of the element by 1. if (!mp.containsKey(arr[i])) mp.put(arr[i], 1); else { int x = mp.get(arr[i]); mp.put(arr[i], ++x); } } for (int i = 0; i < n; ++i) { // Print the element which appear // more than or equal to k times. if (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 // sanjeev2552 |
chevron_right
filter_none
Python3
# Python3 program to remove the elements which # appear strictly less 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 = dict() for i in range(n): # Incrementing the frequency # of the element by 1. mp[arr[i]] = mp.get(arr[i], 0) + 1 for i in range(n): # Print the element which appear # more than or equal to k times. if (arr[i] in mp and mp[arr[i]] >= k): print(arr[i], end = " ") # Driver Code arr = [1, 2, 2, 3, 2, 3, 4] n = len(arr) k = 2removeElements(arr, n, k) # This code is contributed by Mohit Kumar |
chevron_right
filter_none
C#
// C# program to remove the elements which // appear strictly less than k times from the array. using System; using System.Collections.Generic; class geeks { public 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.Add(arr[i], 1); else { int x = mp[arr[i]]; mp[arr[i]] = mp[arr[i]] + ++x; } } for (int i = 0; i < n; ++i) { // Print the element which appear // more than or equal to k times. if (mp[arr[i]] >= k) Console.Write(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
Output:
2 2 3 2 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.
Recommended Posts:
- Remove characters from string that appears strictly less than K times
- Remove elements from the array which appear more than k times
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Remove characters that appear more than k times
- Remove characters from a String that appears exactly K times
- Count the subarray with sum strictly greater than the sum of remaining elements
- Minimum concatenation required to get strictly LIS for array with repetitive elements | Set-2
- Split the array elements into strictly increasing and decreasing sequence
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- 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
- GCD of elements which occur prime number of times
- Unique element in an array where all elements occur K times except one | Set 2
- 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
- GCD of elements occurring Fibonacci number of times in an Array
- Count all elements in the array which appears at least K times after their first occurrence
- Queries to check whether all the elements in the given range occurs even number of times
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
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.

