用set去重,用list排序。
Simple Sorting
https://www.nowcoder.com/practice/139761e0b59a405786898b7f2db9423f
//C++版代码 #include <iostream> #include <vector> #include <unordered_set> #include <algorithm> using namespace std; int main() { int n; unordered_set<int> nums; cin >> n; while (n--) { int num; cin >> num; nums.insert(num); } vector<int> ans(nums.begin(), nums.end()); sort(ans.begin(), ans.end()); for (int num: ans) { cout << num << " "; } return 0; } //Java版代码 import java.util.Arrays; import java.util.HashSet; 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> set = new HashSet<>(); while (n-- != 0) { set.add(sc.nextInt()); } set.stream().sorted().forEach(num -> System.out.print(num + " ")); } } #Python版代码 input() print(*sorted(set(map(int, input().split()))))