📌  相关文章
📜  按相对顺序打印数组中最后一次出现的元素

📅  最后修改于: 2021-10-27 07:01:46             🧑  作者: Mango

给定一个包含 N 个元素的数组,通过删除除最后一个元素之外的所有元素,以与给定的相同相对顺序打印元素。
例子

方法:

  • 散列每个元素的最后一次出现。
  • 在 N 个元素的数组中迭代,如果元素的索引是散列的,则打印数组元素。

下面是上述方法的实现:

C++
// C++ program to print the last occurrence
// of every element in relative order
#include 
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 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 map = new HashMap();
         
        // 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 map = new HashMap();
     
    // 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


Javascript


输出:

5 6 1

时间复杂度: O(N * log N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程