Largest Non | Java program to implement the above approach ; Function to find the largest unique element of the array ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores largest non - repeating element present in the array ; Stores index of the largest non - repeating array element ; Traverse the array ; If frequency of arr [ i ] is equal to 1 and arr [ i ] exceeds LNRElem ; Update ind ; Update LNRElem ; If no array element is found with frequency equal to 1 ; Print largest non - repeating element ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void LarUnEl ( int arr [ ] , int N ) { HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { map . put ( arr [ i ] , map . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } int LNRElem = Integer . MIN_VALUE ; int ind = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( map . get ( arr [ i ] ) == 1 && arr [ i ] > LNRElem ) { ind = i ; LNRElem = arr [ i ] ; } } if ( ind == - 1 ) { System . out . println ( ind ) ; return ; } System . out . println ( arr [ ind ] ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 8 , 8 , 4 } ; int N = arr . length ; LarUnEl ( arr , N ) ; } }