Print the last occurrence of elements in array in relative order

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right



Output:

5 6 1

Time Complexity: O(N * log N)



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.