阅小文有一个长度为n的序列,他想移除掉里面的重复元素,对于每种元素保留最后出现的那个。
阅小文有一个长度为n的序列,他想移除掉里面的重复元素,对于每种元素保留最后出现的那个。
输入包括两行: 第一行为序列长度n(1 ≤ n ≤ 50) 第二行为n个数sequence[i](1 ≤ sequence[i]≤ 100),以空格分隔
输出消除重复元素之后的序列,以空格分隔,行末无空格
9 100 100 100 82 82 82 100 100 100
82 100
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Set<Integer> set = new LinkedHashSet<>(); // 直接存
int n = in.nextInt();
for (int i = 0; i < n; i++) {
int x = in.nextInt();
if (set.contains(x)) {
set.remove(x);
}
set.add(x);
}
int idx = 0;
for (int key : set) {
if (idx == set.size()-1) System.out.println(key);
else System.out.print(key + " ");
idx++;
}
}
}