Largest Non | C ++ program to implement the above approach ; Function to find the largest unique element of the array ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Stores largest non - repeating element present in the array ; Stores index of the largest unique element of the array ; 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 the largest non - repeating element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void LarUnEl ( int arr [ ] , int N ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] ++ ; } int LNRElem = INT_MIN ; int ind = -1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp [ arr [ i ] ] == 1 && arr [ i ] > LNRElem ) { ind = i ; LNRElem = arr [ i ] ; } } if ( ind == -1 ) { cout << ind ; return ; } cout << arr [ ind ] ; } int main ( ) { int arr [ ] = { 3 , 1 , 8 , 8 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; LarUnEl ( arr , N ) ; }