Java写题解的第1天:提取不重复的整数

由“不重复”联想到需要使用set,由按顺序输出联想到使用LinkedHashSet;(如果需要排序再输出,那么就需要考虑TreeSet)
既然题目要求输入int整数,那么从后往前遍历这个整数的所有位数,就可以考虑先获取除以10的余数,再将其个位数截掉(n /= 10
如果不考虑输入的是整数,比如字符串,那么就可以直接从后往前遍历

import java.io.*;
import java.util.LinkedHashSet;
import java.util.Iterator;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        br.close();
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
        while(n > 0) {
            set.add(n % 10);
            n /= 10;
        }
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next());
        }
    }
}