Largest number in the Array having frequency same as value | Java solution to the above problem ; Function to find the largest number whose frequency is equal to itself . ; Find the maximum element in the array ; Driver code
import java . util . * ; class GFG { static int findLargestNumber ( int [ ] arr ) { int k = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int [ ] m = new int [ k + 1 ] ; for ( int n : arr ) ++ m [ n ] ; for ( int n = arr . length - 1 ; n > 0 ; -- n ) { if ( n == m [ n ] ) return n ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 5 , 2 , 4 , 5 } ; System . out . print ( findLargestNumber ( arr ) + "NEW_LINE"); } }