{ }codelper
集合添加元素P0035

删除重复的数

第一行给出整数个数 n,第二行给出 n 个整数。
删去前面已经出现过的值,按首次出现的顺序把剩下的值用一个空格隔开输出在一行(3 1 3 2 1 得 3 1 2)。

输入
1 ≤ n ≤ 1000,-1,000,000,000 ≤ 每个整数 ≤ 1,000,000,000
输出
去重后的列表,一行
样例 1 · stdin
5
3 1 3 2 1
stdout
3 1 2
main.py
输入(stdin)

运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。

$ 就绪

请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。

其他语言怎么写?查看各语言题解 · 8 种语言题解

集合能快速告诉你“见过没有”,但是否保持顺序因语言而异。
Python set、C# HashSet、C++ unordered_set 没有确定的顺序,C++ std::set 按大小排序。
所以集合只用来检查,结果另外放进列表(C# if (seen.Add(x)),C++ seen.insert(x).second)。
也有保持插入顺序的集合:JS new Set,Java LinkedHashSet,Python dict.fromkeys(a)。
C 没有集合,只能从头查找已保存的值(n 为 1000 时足够快)。
Craykon 用 字典() 的 包含 检查,用 result.添加(x) 收集。

Pythonmain.py · 8 行
n = int(input())seen = set()result = []for x in input().split():    if x not in seen:        seen.add(x)        result.append(x)print(" ".join(result))
JavaScriptmain.js · 2 行
const [n, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log([...new Set(a)].join(" "));
TypeScriptmain.ts · 3 行
import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log([...new Set<number>(a)].join(" "));
JavaMain.java · 16 行
import java.util.LinkedHashSet;import java.util.Scanner;import java.util.Set;import java.util.StringJoiner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        Set<Integer> seen = new LinkedHashSet<>();        for (int i = 0; i < n; i++) seen.add(sc.nextInt());        StringJoiner out = new StringJoiner(" ");        for (int x : seen) out.add(String.valueOf(x));        System.out.println(out);    }}
C#Main.cs · 18 行
using System;using System.Collections.Generic; class Program{    static void Main()    {        Console.ReadLine();        var seen = new HashSet<int>();        var result = new List<int>();        foreach (string s in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries))        {            int x = int.Parse(s);            if (seen.Add(x)) result.Add(x);        }        Console.WriteLine(string.Join(" ", result));    }}
C++main.cpp · 16 行
#include <iostream>#include <unordered_set>#include <vector> int main() {    int n, x;    std::cin >> n;    std::unordered_set<int> seen;    std::vector<int> result;    for (int i = 0; i < n; i++) {        std::cin >> x;        if (seen.insert(x).second) result.push_back(x);    }    for (size_t i = 0; i < result.size(); i++) std::cout << (i ? " " : "") << result[i];    std::cout << "\n";}
Cmain.c · 16 行
#include <stdio.h> int main(void) {    int n, x, result[1000], m = 0;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        scanf("%d", &x);        int found = 0;        for (int j = 0; j < m && !found; j++)            if (result[j] == x) found = 1;        if (!found) result[m++] = x;    }    for (int i = 0; i < m; i++) printf(i ? " %d" : "%d", result[i]);    putchar('\n');    return 0;}
Craykonmain.cray · 13 行
n = 控制台.读行().转数字();a = 控制台.读数字();seen = 字典();result = [];遍历 (x 在 a){    如果 (!seen.包含(x))    {        seen[x] = 真;        result.添加(x);    }}控制台.输出(result);