3477. 简单排序
给定一个包含 nn 个整数的数组,请你删除数组中的重复元素并将数组从小到大排序后输出。
输入格式
第一行包含一个整数 nn。
第二行包含 nn 个不超过 10001000 的正整数。
输出格式
输出去重和排序完毕后的数组。
数据范围
1≤n≤10001≤n≤1000
输入样例:
6
8 8 7 3 7 7
输出样例:
3 7 8
题解TreeSet:
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
public static int b;
public static void main(String[] args)throws Exception {
Scanner scanner = new Scanner(System.in);
TreeSet<Integer> set = new TreeSet<>();
int n = scanner.nextInt();
while (n --> 0){
set.add(scanner.nextInt());
}
for (Integer integer : set) {
System.out.printf("%d ",integer);
}
}
}
题解HashSet:
import java.util.*;
public class Main {
public static void main(String[] args)throws Exception {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Set<Integer> set = new HashSet<>();
while (n --> 0){
set.add(scanner.nextInt());
}
Integer[] temp = set.toArray(new Integer[]{});
Arrays.sort(temp);
for (Integer integer : temp) {
System.out.printf("%d ",integer);
}
}
}
评论区