import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); boolean[] exists = new boolean[501]; // 使用501以保证能存储500 for (int i = 0; i < n; i++) { int num = in.nextInt(); exists[num] = true; } for (int i = 1; i <= 500; i++) { if (exists[i]) { System.out.println(i); } } } }
https://www.nowcoder.com/link/spring1
- 读取输入:用Scanner读取输入的整数n,然后读取接下来的n个整数。
- 去重处理:创建一个长度为501的数组exists,用于记录每个数字是否出现。每读取一个数字,将对应索引位置的布尔值设为true,自动去重。
- 排序输出:遍历布尔数组,从索引1到500,输出所有值为true的索引,即得到去重且排序后的结果。