Remove elements from the array which appear more than k times | C ++ program to remove the elements which appear more than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear less than or equal to k times . ; Driver code
#include " iostream " NEW_LINE #include " unordered _ map " NEW_LINE 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 ; }