Remove elements that appear strictly less than k times
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 appear strictly less than k times.
- Else, print it.
Implementation:
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 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;
class geeks
{
public static void removeElements( int [] arr,
int n, int k)
{
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; ++i)
{
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)
{
if (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 = dict ()
for i in range (n):
mp[arr[i]] = mp.get(arr[i], 0 ) + 1
for i in range (n):
if (arr[i] in mp and mp[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;
class geeks
{
public 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.Add(arr[i], 1);
else
{
int x = mp[arr[i]];
mp[arr[i]] = mp[arr[i]] + ++x;
}
}
for ( int i = 0; i < n; ++i)
{
if (mp[arr[i]] >= k)
Console.Write(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);
}
}
|
Javascript
<script>
function removeElements(arr,n,k)
{
let mp = new Map();
for (let i = 0; i < n; ++i)
{
if (!mp.has(arr[i]))
mp.set(arr[i], 1);
else
{
let x = mp.get(arr[i]);
mp.set(arr[i], ++x);
}
}
for (let i = 0; i < n; ++i)
{
if (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)
Auxiliary Space: O(n)
Method #2:Using Built-in Python functions:
- Count the frequencies of every element using Counter() function
- Traverse the array.
- Remove the elements which appear strictly less than k times.
- Else, print it.
Implementation:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
void removeElements( int arr[], int n, int k)
{
unordered_map< int , int > freq;
for ( int i = 0; i < n; i++) {
if (freq.count(arr[i])) {
freq[arr[i]]++;
}
else {
freq[arr[i]] = 1;
}
}
for ( int i = 0; i < n; i++) {
if (freq[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;
}
|
Java
import java.util.*;
public class Main {
public static void removeElements( int [] arr, int n, int k) {
HashMap<Integer, Integer> freq = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
if (freq.containsKey(arr[i])) {
freq.put(arr[i], freq.get(arr[i]) + 1 );
}
else {
freq.put(arr[i], 1 );
}
}
for ( int i = 0 ; i < n; i++) {
if (freq.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
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;
public class Program
{
public static void removeElements( int [] arr, int n, int k)
{
Dictionary< int , int > freq = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (freq.ContainsKey(arr[i]))
{
freq[arr[i]] = freq[arr[i]] + 1;
}
else
{
freq.Add(arr[i], 1);
}
}
for ( int i = 0; i < n; i++)
{
if (freq[arr[i]] >= k)
{
Console.Write(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);
}
}
|
Javascript
let x= "" ;
function removeElements(arr, n, k) {
const freq = arr.reduce((count, val) => {
count[val] = (count[val] || 0) + 1;
return count;
}, {});
for (let i = 0; i < n; i++) {
if (freq[arr[i]] >= k) {
x = x+ arr[i] + " " ;
}
}
console.log(x);
}
const arr = [1, 2, 2, 3, 2, 3, 4];
const n = arr.length;
const k = 2;
removeElements(arr, n, k);
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3:( Space optimization): First we will sort the array and then check if the frequency of array element is greater than or equal to k with the use of binary search function ( Upper_bound ) . Frequency of array element is ‘last_index-first_index+1’ . If the frequency is greater than one , then print it .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void removeElements( int arr[], int n, int k)
{
sort(arr, arr + n);
for ( int i = 0; i < n; i++) {
int first_index
= lower_bound(arr, arr + n, arr[i]) - arr;
int last_index
= upper_bound(arr, arr + n, arr[i]) - arr - 1;
int fre = last_index - first_index
+ 1;
if (fre >= 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;
}
|
Javascript
function lower_bound(a, x){
let l = 0;
let h = arr.length - 1;
while (l <= h){
let m = Math.floor((l + h)/2);
if (a[m] < x){
l = m + 1;
}
else if (a[m] >= x){
h = m - 1;
}
}
return l;
}
function upper_bound(a, x){
let l = 0;
let h = arr.length - 1;
while (l <= h){
let m = Math.floor((l + h)/2);
if (a[m] <= x){
l = m + 1;
}
else if (a[m] > x){
h = m - 1;
}
}
return l;
}
function removeElements(arr, n, k)
{
arr.sort();
for (let i = 0 ; i < n ;i++)
{
let first_index = lower_bound(arr, arr[i]);
let last_index = upper_bound(arr, arr[i])- 1;
let fre = last_index-first_index+1;
if (fre >= k)
{
process.stdout.write(arr[i] + " " );
}
}
}
let arr = [1, 2, 2, 3, 2, 3, 4];
let n = arr.length;
let k = 2;
removeElements(arr, n, k);
|
Python3
def removeElements(arr, n, k):
arr.sort()
for i in range (n):
first_index = bisect.bisect_left(arr, arr[i])
last_index = bisect.bisect_right(arr, arr[i]) - 1
fre = last_index - first_index + 1
if fre > = k:
print (arr[i], end = " " )
if __name__ = = "__main__" :
import bisect
arr = [ 1 , 2 , 2 , 3 , 2 , 3 , 4 ]
n = len (arr)
k = 2
removeElements(arr, n, k)
|
Java
import java.util.*;
class Main {
static void removeElements( int arr[], int n, int k)
{
Arrays.sort(arr);
ArrayList<Integer> arr1 = new ArrayList<Integer>( 5 );
for ( int i = 0 ; i < n; i++) {
arr1.add(arr[i]);
}
for ( int i = 0 ; i < n; i++) {
int first_index = arr1.indexOf(arr[i]);
int last_index = arr1.lastIndexOf(arr[i]);
while (last_index < n
&& arr[last_index] == arr[i])
last_index++;
last_index--;
int fre = last_index - first_index
+ 1 ;
if (fre >= 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);
}
}
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
static void RemoveElements( int [] arr, int n, int k)
{
Array.Sort(arr);
List< int > arr1 = new List< int >(5);
for ( int i = 0; i < n; i++) {
arr1.Add(arr[i]);
}
for ( int i = 0; i < n; i++) {
int first_index = arr1.IndexOf(arr[i]);
int last_index = arr1.LastIndexOf(arr[i]);
while (last_index < n && arr[last_index] == arr[i])
last_index++;
last_index--;
int fre = last_index - first_index + 1;
if (fre >= k) {
Console.Write(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);
}
}
|
Time Complexity: O(n*log2n)
Auxiliary Space: O(1)
Last Updated :
06 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...