原文:

给定一个由 n 个元素组成的数组,按照给定的相对顺序打印元素,删除除最后一次出现的元素之外的所有出现的元素。 :

输入: a[] = {1,5,5,1,6,1} 输出: 5 6 1 去掉两个整数 1,在位置 1 和 4。另外,删除整数 5,它位于位置 2。 因此左数组为{5,6,1} 输入: a[] = {2,5,5,2} 输出: 5 2

进场:

  • 散列每个元素的最后一次出现。
  • 迭代 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

python 3

# 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

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

5 6 1

时间复杂度: o(n * log n)