# Last seen array element (last appearance is earliest)

Given an array that might contain duplicates, find the element whose last appearance is latest.

**Examples:**

Input : arr[] = {10, 30, 20, 10, 20} Output : 30 Explanation: Below are indexes of last appearances of all elements (0 based indexes) 10 last occurs at index 3 30 last occurs at index 1 20 last occurs at index 2 The element whose last appearance earliest is 30. Input : arr[] = {20, 10, 20, 20, 40, 10} Output : 20 Explanation: Explanation: Below are indexes of last appearances of all elements (0 based indexes) 20 last occurs at index 2 10 last occurs at index 5 40 last occurs at index 4 The element whose last appearance earliest is 20.

A **naive approach** is to take every element and iterate and compare its last occurrence with other elements. This requires two nested loops, and will take O(n*n) time.

An **efficient approach** is to use hashing. We store every elements index where it last occurred, and then iterate through all the possible elements and print the element with the least least index stored occurrence, as that will be the one which was last seen while traversing from left to right.

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java program to find last seen element in ` `// an array. ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns last seen element in arr[] ` `static` `int` `lastSeenElement(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Store last occurrence index of ` ` ` `// every element ` ` ` `HashMap<Integer, ` ` ` `Integer> hash = ` `new` `HashMap<Integer, ` ` ` `Integer>(); ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `hash.put(a[i], i); ` ` ` `} ` ` ` ` ` `// Find an element in hash with minimum ` ` ` `// index value ` ` ` `int` `res_ind = Integer.MAX_VALUE, res = ` `0` `; ` ` ` `for` `(Map.Entry<Integer, ` ` ` `Integer> x : hash.entrySet()) ` ` ` `{ ` ` ` `if` `(x.getValue() < res_ind) ` ` ` `{ ` ` ` `res_ind = x.getValue(); ` ` ` `res = x.getKey(); ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a[] = { ` `2` `, ` `1` `, ` `2` `, ` `2` `, ` `4` `, ` `1` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.println(lastSeenElement(a, n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find last seen element in ` `// an array. ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns last seen element in arr[] ` `static` `int` `lastSeenElement(` `int` `[]a, ` `int` `n) ` `{ ` ` ` `// Store last occurrence index of ` ` ` `// every element ` ` ` `Dictionary<` `int` `, ` ` ` `int` `> hash = ` `new` `Dictionary<` `int` `, ` ` ` `int` `>(); ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(hash.ContainsKey(a[i])) ` ` ` `{ ` ` ` `hash[a[i]] = i; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `hash.Add(a[i], i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Find an element in hash with minimum ` ` ` `// index value ` ` ` `int` `res_ind = ` `int` `.MaxValue, res = 0; ` ` ` `foreach` `(KeyValuePair<` `int` `, ` `int` `> x ` `in` `hash) ` ` ` `{ ` ` ` `if` `(x.Value < res_ind) ` ` ` `{ ` ` ` `res_ind = x.Value; ` ` ` `res = x.Key; ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]a = { 2, 1, 2, 2, 4, 1 }; ` ` ` `int` `n = a.Length; ` ` ` `Console.WriteLine(lastSeenElement(a, n)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find Array formed by adding each element of given array with largest element in new array to its left
- Largest element smaller than current element on left for every element in Array
- Array formed from difference of each element from the largest element in the given array
- Find just strictly greater element from first array for each element in second array
- Form an array of distinct elements with each element as sum of an element from each array
- Closest greater element for every array element from another array
- Replace each element by the difference of the total size of the array and frequency of that element
- Range Query on array whose each element is XOR of index value and previous element
- Check if minimum element in array is less than or equals half of every other element
- Replace every array element by Bitwise Xor of previous and next element
- Find last element after deleting every second element in array of n integers
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Maximum possible remainder when an element is divided by other element in the array
- Replace every element of the array by its previous element
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Find an element in array such that sum of left array is equal to sum of right array
- Replace every element of the array by its next element
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Generate Array whose difference of each element with its left yields the given Array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.