The first line gives the count n, and the second line gives n integers. Remove every value that already appeared earlier, and print the remaining values in their original order on one line, separated by single spaces (3 1 3 2 1 becomes 3 1 2).
Input
1 ≤ n ≤ 1000, -1,000,000,000 ≤ each integer ≤ 1,000,000,000
Output
the list without duplicates on one line
Example 1 · stdin
5
3 1 3 2 1
stdout
3 1 2
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
A set tells you quickly whether you have seen a value, but whether it keeps order depends on the language. Python set, C# HashSet and C++ unordered_set have no defined order, and C++ std::set is sorted. So use the set only for checking and collect the result in a list (C# if (seen.Add(x)), C++ seen.insert(x).second). Some sets do keep insertion order: JS new Set, Java LinkedHashSet, Python dict.fromkeys(a). C has no set, so it searches the values kept so far from the start (fast enough for n = 1000). Craykon checks with Contains on a Dict() and collects with result.Add(x).
Pythonmain.py · 8 lines
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))
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 lines
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 lines
#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 lines
#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 lines
n = Console.ReadLine().ToNumber();a = Console.ReadNumbers();seen = Dict();result = [];foreach (x in a){ if (!seen.Contains(x)) { seen[x] = true; result.Add(x); }}Console.WriteLine(result);