Remove array elements to reduce frequency of each array element to at most K | Function to remove array elements such that frequency of each distinct array element is at most K ; Base Case ; Stores index of array element by removing the array element such that the frequency of array elements is at most K ; Traverse the array ; If j < k or arr [ i ] > arr [ j - k ] ; Update arr [ j ] ; Remove array elements ; Function to print the array ; Traverse the array ; Utility Function to remove array elements such that frequency of each distinct array element is at most K ; Print updated array ; Driver code
function RemoveElemArr ( arr , n , k ) { if ( n == 0 n == 1 ) return arr ; var j = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( j < k arr [ i ] > arr [ j - k ] ) { arr [ j ++ ] = arr [ i ] ; } } while ( arr . length > j ) { arr . pop ( ) ; } return arr ; } function printArray ( arr ) { for ( var i = 0 ; i < arr . length ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function UtilRemov ( arr , n , k ) { arr = RemoveElemArr ( arr , n , k ) ; printArray ( arr ) ; } var arr = [ 1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 ] ; var k = 2 ; var n = arr . length ; UtilRemov ( arr , n , k ) ;