Read the question, write Java on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Read n then n ints; print unique values ascending, space-separated.
Examples
Input
5 3 1 3 2 1
Output
1 2 3
Hint
- TreeSet keeps unique sorted order.
Show correct code
Peek only after you have tried. You can still Check your own version.
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < n; i++) set.add(in.nextInt());
boolean first = true;
for (int x : set) {
if (!first) System.out.print(' ');
System.out.print(x);
first = false;
}
System.out.println();
}
}
Main.javaJava 21 · javac · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.