Unique Values
Completed
Read n integers. Print how many distinct values there are, then the distinct values in the order they first appeared (use a LinkedHashSet).
Input
n, then n integers.
Output
Two lines.
Example
Input
7 3 1 3 2 1 5 3
Output
4 3 1 2 5
Editor
Java
Hints
A Set ignores duplicates.
LinkedHashSet remembers insertion order.
Solution explanation
HashSet = fast, no order; LinkedHashSet = insertion order; TreeSet = sorted order.
Show solution
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Set<Integer> seen = new LinkedHashSet<Integer>();
for (int i = 0; i < n; i++) {
seen.add(sc.nextInt());
}
System.out.println(seen.size());
StringBuilder sb = new StringBuilder();
for (int x : seen) {
sb.append(sb.length() > 0 ? " " : "").append(x);
}
System.out.println(sb);
}
}