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
- 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++
#include "iostream"
#include "unordered_map"
using namespace std;
void RemoveElements( int arr[], int n, int k)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; ++i) {
mp[arr[i]]++;
}
for ( int i = 0; i < n; ++i) {
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
import java.util.HashMap;
import java.util.Map;
class GFG
{
static void RemoveElements( int arr[], int n, int k)
{
Map<Integer,Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; ++i)
{
mp.put(arr[i],mp.get(arr[i]) == null ? 1 :mp.get(arr[i])+ 1 );
}
for ( int i = 0 ; i < n; ++i)
{
if (mp.containsKey(arr[i]) && mp.get(arr[i]) <= k)
{
System.out.print(arr[i] + " " );
}
}
}
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);
}
}
|
Python3
def RemoveElements(arr, n, k):
mp = {i: 0 for i in range ( len (arr))}
for i in range (n):
mp[arr[i]] + = 1
for i in range (n):
if (mp[arr[i]] < = k):
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 2 , 2 , 3 , 2 , 3 , 4 ]
n = len (arr)
k = 2
RemoveElements(arr, n, k)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void RemoveElements( int [] arr,
int n, int k)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; ++i)
{
if (mp.ContainsKey(arr[i]))
mp[arr[i]]++;
else
mp[arr[i]] = 1;
}
for ( int i = 0; i < n; ++i)
{
if (mp.ContainsKey(arr[i]) && mp[arr[i]] <= k)
{
Console.Write(arr[i] + " " );
}
}
}
static public void Main()
{
int [] arr = { 1, 2, 2, 3, 2, 3, 4 };
int n = arr.Length;
int k = 2;
RemoveElements(arr, n, k);
}
}
|
Javascript
<script>
function RemoveElements(arr,n,k)
{
let mp = new Map();
for (let i = 0; i < n; ++i)
{
mp.set(arr[i],mp.get(arr[i]) == null ?1:mp.get(arr[i])+1);
}
for (let i = 0; i < n; ++i)
{
if (mp.has(arr[i]) && mp.get(arr[i]) <= k)
{
document.write(arr[i] + " " );
}
}
}
let arr=[1, 2, 2, 3, 2, 3, 4 ];
let n = arr.length;
let k = 2;
RemoveElements(arr, n, k);
</script>
|
Time Complexity – O(N), where N is the size of the given integer.
Auxiliary Space – O(N), where N is the size of the given integer.
Method #2:Using Built-in Python functions:
- 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:
C++
#include <iostream>
#include <unordered_map>
#include <vector>
void removeElements( const std::vector< int >& arr, int k) {
std::unordered_map< int , int > freq;
for ( int i = 0; i < arr.size(); ++i) {
freq[arr[i]]++;
}
for ( int i = 0; i < arr.size(); ++i) {
if (freq[arr[i]] <= k) {
std::cout << arr[i] << " " ;
}
}
}
int main() {
std::vector< int > arr = {1, 2, 2, 3, 2, 3, 4};
int k = 2;
removeElements(arr, k);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void
removeElements(ArrayList<Integer> arr, int k)
{
Map<Integer, Integer> freq = new HashMap<>();
for ( int i = 0 ; i < arr.size(); ++i) {
freq.put(arr.get(i),
freq.getOrDefault(arr.get(i), 0 ) + 1 );
}
for ( int i = 0 ; i < arr.size(); ++i) {
if (freq.get(arr.get(i)) <= k) {
System.out.print(arr.get(i) + " " );
}
}
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<>();
arr.add( 1 );
arr.add( 2 );
arr.add( 2 );
arr.add( 3 );
arr.add( 2 );
arr.add( 3 );
arr.add( 4 );
int k = 2 ;
removeElements(arr, k);
}
}
|
Python3
from collections import Counter
def removeElements(arr, n, k):
freq = Counter(arr)
for i in range (n):
if (freq[arr[i]] < = k):
print (arr[i], end = " " )
arr = [ 1 , 2 , 2 , 3 , 2 , 3 , 4 ]
n = len (arr)
k = 2
removeElements(arr, n, k)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void RemoveElements(List< int > arr, int k)
{
Dictionary< int , int > freq = new Dictionary< int , int >();
foreach ( int element in arr)
{
if (freq.ContainsKey(element))
{
freq[element]++;
}
else
{
freq[element] = 1;
}
}
foreach ( int element in arr)
{
if (freq[element] <= k)
{
Console.Write(element + " " );
}
}
}
static void Main()
{
List< int > arr = new List< int > { 1, 2, 2, 3, 2, 3, 4 };
int k = 2;
RemoveElements(arr, k);
Console.ReadLine();
}
}
|
Javascript
function removeElements(arr, k) {
const freq = new Map();
for (let i = 0; i < arr.length; ++i) {
freq.set(arr[i], (freq.get(arr[i]) || 0) + 1);
}
for (let i = 0; i < arr.length; ++i) {
if (freq.get(arr[i]) <= k) {
console.log(arr[i] + " " );
}
}
}
const arr = [1, 2, 2, 3, 2, 3, 4];
const k = 2;
removeElements(arr, k);
|
Time Complexity – O(N), where N is the size of the given integer.
Auxiliary Space – O(N), where N is the size of the given integer.