Print the last occurrence of elements in array in relative order Last Updated : 31 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given an array of N elements, print the elements in the same relative order as given by removing all the occurrences of elements except the last occurrence. Examples: Input: a[] = {1, 5, 5, 1, 6, 1} Output: 5 6 1 Remove two integers 1, which are in the positions 1 and 4. Also, remove the integer 5, which is in the position 2. Hence the left array is {5, 6, 1} Input: a[] = {2, 5, 5, 2} Output: 5 2 Approach: Hash the last occurrence of every element.Iterate in the array of N elements, if the element's index is hashed, then print the array element. Below is the implementation of the above approach: C++ // C++ program to print the last occurrence // of every element in relative order #include <bits/stdc++.h> using namespace std; // Function to print the last occurrence // of every element in an array void printLastOccurrence(int a[], int n) { // used in hashing unordered_map<int, int> mp; // iterate and store the last index // of every element for (int i = 0; i < n; i++) mp[a[i]] = i; // iterate and check for the last // occurrence of every element for (int i = 0; i < n; i++) { if (mp[a[i]] == i) cout << a[i] << " "; } } // Driver Code int main() { int a[] = { 1, 5, 5, 1, 6, 1 }; int n = sizeof(a) / sizeof(a[0]); printLastOccurrence(a, n); return 0; } Java // Java program to print the // last occurrence of every // element in relative order import java.util.*; class GFG { // Function to print the last // occurrence of every element // in an array public static void printLastOccurrence(int a[], int n) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); // iterate and store the last // index of every element for (int i = 0; i < n; i++) map.put(a[i], i); for (int i = 0; i < n; i++) { if (map.get(a[i]) == i) System.out.print(a[i] +" "); } } // Driver Code public static void main (String[] args) { int a[] = { 1, 5, 5, 1, 6, 1 }; int n = a.length; printLastOccurrence(a, n); } } // This code is contributed // by ankita_saini Python3 # Python 3 program to print the last occurrence # of every element in relative order # Function to print the last occurrence # of every element in an array def printLastOccurrence(a, n): # used in hashing mp = {i:0 for i in range(7)} # iterate and store the last # index of every element for i in range(n): mp[a[i]] = i # iterate and check for the last # occurrence of every element for i in range(n): if (mp[a[i]] == i): print(a[i], end = " ") # Driver Code if __name__ == '__main__': a = [1, 5, 5, 1, 6, 1] n = len(a) printLastOccurrence(a, n) # This code is contributed by # Surendra_Gangwar C# // C# program to print the // last occurrence of every // element in relative order using System; class GFG { // Function to print the last // occurrence of every element // in an array public static void printLastOccurrence(int[] a, int n) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); // iterate and store the last // index of every element for (int i = 0; i < n; i++) map.put(a[i], i); for (int i = 0; i < n; i++) { if (map.get(a[i]) == i) Console.Write(a[i] + " "); } } // Driver Code public static void Main () { int[] a = { 1, 5, 5, 1, 6, 1 }; int n = a.Length; printLastOccurrence(a, n); } } // This code is contributed // by ChitraNayal PHP <?php // PHP program to print the last // occurrence of every element // in relative order // Function to print the last // occurrence of every element // in an array function printLastOccurrence(&$a, $n) { // used in hashing $mp = array(); // iterate and store the last // index of every element for ($i = 0; $i < $n; $i++) $mp[$a[$i]] = $i; // iterate and check for the last // occurrence of every element for ($i = 0; $i < $n; $i++) { if ($mp[$a[$i]] == $i) echo $a[$i] . " "; } } // Driver Code $a = array(1, 5, 5, 1, 6, 1); $n = sizeof($a); printLastOccurrence($a, $n); // This code is contributed // by ChitraNayal ?> JavaScript <script> // Javascript program to print the last // occurrence of every element // in relative order // Function to print the last // occurrence of every element // in an array function printLastOccurrence(a, n) { // used in hashing let mp = []; // iterate and store the last // index of every element for (let i = 0; i < n; i++) mp[a[i]] = i; // iterate and check for the last // occurrence of every element for (let i = 0; i < n; i++) { if (mp[a[i]] == i) document.write( a[i] + " "); } } // Driver Code let a = [1, 5, 5, 1, 6, 1]; let n = a.length; printLastOccurrence(a, n); // This code is contributed by sravan kumar </script> Output5 6 1 Complexity Analysis: Time Complexity: O(N)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Print the last occurrence of elements in array in relative order S Striver Follow Improve Article Tags : Misc Hash DSA Arrays cpp-unordered_map +1 More Practice Tags : ArraysHashMisc Similar Reads Find the Kth occurrence of an element in a sorted Array Given a sorted array arr[] of size N, an integer X, and a positive integer K, the task is to find the index of Kth occurrence of X in the given array. Examples: Input: N = 10, arr[] = [1, 2, 3, 3, 4, 5, 5, 5, 5, 5], X = 5, K = 2Output: Starting index of the array is '0' Second occurrence of 5 is at 15+ min read Elements that occurred only once in the array Given an array arr that has numbers appearing twice or once. The task is to identify numbers that occur only once in the array. Note: Duplicates appear side by side every time. There might be a few numbers that can occur at one time and just assume this is a right rotating array (just say an array c 15+ min read Sort the linked list in the order of elements appearing in the array Given an array of size N and a Linked List where elements will be from the array but can also be duplicated, sort the linked list in the order, elements are appearing in the array. It may be assumed that the array covers all elements of the linked list.arr[] = list = Sorted list = Asked in Amazon Fi 9 min read Find the first repeating element in an array of integers Given an array of integers arr[], The task is to find the index of first repeating element in it i.e. the element that occurs more than once and whose index of the first occurrence is the smallest. Examples: Input: arr[] = {10, 5, 3, 4, 3, 5, 6}Output: 5 Explanation: 5 is the first element that repe 8 min read Print n smallest elements from given array in their original order We are given an array of m-elements, we need to find n smallest elements from the array but they must be in the same order as they are in given array. Examples: Input : arr[] = {4, 2, 6, 1, 5}, n = 3 Output : 4 2 1 Explanation : 1, 2 and 4 are 3 smallest numbers and 4 2 1 is their order in given arr 5 min read Like