Largest number in the Array having frequency same as value | Function to find the largest number whose frequency is equal to itself . ; Find the maximum element in the array ; Driver code
function findLargestNumber ( arr ) { var k = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var m = Array ( k ) . fill ( 0 ) ; arr . forEach ( n => { ++ m [ n ] ; } ) ; for ( var n = arr . length ; n > 0 ; -- n ) { if ( n == m [ n ] ) return n ; } return - 1 ; } var arr = [ 3 , 2 , 5 , 2 , 4 , 5 ] ; document . write ( findLargestNumber ( arr ) ) ;