Largest element in the array that is repeated exactly k times | Function that finds the largest element that occurs exactly ' k ' times ; store the frequency of each element ; to store the maximum element ; if current element has frequency ' k ' and current maximum hasn 't been set ; set the current maximum ; if current element has frequency ' k ' and it is greater than the current maximum ; change the current maximum ; if there is no element with frequency ' k ' ; print the largest element with frequency ' k ' ; Driver code ; find the largest element that is repeated K times
function solve ( arr , n , k ) { var m = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } var max = - 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { if ( m . get ( arr [ i ] ) == k && max == - 1000000000 ) { max = arr [ i ] ; } else if ( m . get ( arr [ i ] ) == k && max < arr [ i ] ) { max = arr [ i ] ; } } if ( max == - 1000000000 ) document . write ( " " ) ; else document . write ( max ) ; } var arr = [ 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ] ; var k = 4 ; var n = arr . length ; solve ( arr , n , k ) ;