Largest number in the Array having frequency same as value | Function to find the largest number whose frequency is equal to itself . ; Adding 65536 to keep the count of the current number ; right shifting by 16 bits to find the count of the number i ; Driver code
function findLargestNumber ( arr ) { arr . forEach ( n => { n &= 0xFFFF ; if ( n <= arr . length ) { arr [ n - 1 ] += 0x10000 ; } } ) ; for ( var i = arr . length ; i > 0 ; -- i ) { if ( ( arr [ i - 1 ] >> 16 ) == i ) return i ; } return - 1 ; } var arr = [ 3 , 2 , 5 , 5 , 2 , 4 , 5 ] ; document . write ( findLargestNumber ( arr ) )