Duplicated Numbers 分数 10 作者 翁恺 单位 浙江大学 This program reads a lot of integers, in which may be duplicated numbers. The program picks out all the duplicated ones and sorts the remainders in a descendent order. 函数接口定义: public static ArrayList<Integer> pick(ArrayList<Integer> a); a is the ArrayList to be parsed and returns the result as an ArrayList. Hint: These classes and functions may be used. Collection Collections.sort(Collection); Collection Collections.reverse(Collection) 裁判测试程序样例: import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; public class Main { /* 请在这里填写答案 */ public static void main(String[] args) { ArrayList<Integer> lst = new ArrayList<>(); Scanner in = new Scanner(System.in); while ( in.hasNextInt() ) { lst.add(in.nextInt()); } lst = pick(lst); for ( int x:lst) { System.out.print(x+" "); } System.out.println(); in.close(); } }
时间: 2024-02-18 16:01:54 浏览: 204
yarn-deduplicate:用于yarn.lock文件的重复数据删除工具
下面是实现题目要求的代码:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
public class Main {
public static ArrayList<Integer> pick(ArrayList<Integer> a) {
ArrayList<Integer> result = new ArrayList<Integer>();
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < a.size(); i++) {
if (set.contains(a.get(i))) {
result.add(a.get(i));
} else {
set.add(a.get(i));
}
}
Collections.sort(result);
Collections.reverse(result);
return result;
}
public static void main(String[] args) {
ArrayList<Integer> lst = new ArrayList<>();
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
lst.add(in.nextInt());
}
lst = pick(lst);
for (int x : lst) {
System.out.print(x + " ");
}
System.out.println();
in.close();
}
}
```
阅读全文