Last seen array element ( last appearance is earliest ) | C ++ program to find last seen element in an array . ; Returns last seen element in arr [ ] ; Store last occurrence index of every element ; Find an element in hash with minimum index value ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastSeenElement ( int a [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ a [ i ] ] = i ; int res_ind = INT_MAX , res ; for ( auto x : hash ) { if ( x . second < res_ind ) { res_ind = x . second ; res = x . first ; } } return res ; } int main ( ) { int a [ ] = { 2 , 1 , 2 , 2 , 4 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << lastSeenElement ( a , n ) ; return 0 ; }