高分悬赏:Java语言如何才能计算一个20位数中有哪些数字是没有重复过的

高分悬赏:Java语言如何才能计算一个20位数中有哪些数字是没有重复过的
高分悬赏:Java语言如何才能计算一个20位数中有哪些数字是没有重复过的

首先将数值转换为字符串,然后遍历字符串各个字符,用 Set 计划判断,如果集合中存中,就是重复数字了。
使用 JDK 的库函数有,整后就能得到了。

public static void main(String[] args) {
        String number="12346575757575757589";
        int foot=-1;
        for(int i=0;i<10;i++){
            if(number.contains(i+"")){
                foot=number.indexOf(i+"");
                if(number.indexOf(i+"", foot+1)==-1){
                    System.out.println(i+"");
                }
                else{
                    continue;
                }
            }
            else{
                continue;
            }
        }
    }

输出结果:

1
2
3
4
6
8
9

灵活使用字符串分割,按照该数字分割,如果该数字没出现过则返回的数组长度为1,出现过1次则返回数组的长度为2,出现n次则返回数组长度为n+1

public class Sample {
    public static void main(String[] args) {
        String s = "12346789123456789012";
        for (int i=0; i<9; i++) {
            String regx = String.format("%d", i); 
            if (s.split(regx, -1).length==2) {
                System.out.printf("[%d]没有重复\n", i);
            }
        }
    } 
}

利用叠加方式

String a="12233344455677890";
Map<String,Integer> map = new HashMap<>();
for (String a1:a.split("")) {
    map.put(a1,map.getOrDefault(a1,0)+1);
}
for (Map.Entry<String,Integer> str : map.entrySet()) {
    if(str.getValue().equals(1)){
        System.out.println(str.getKey());
    }
}

用一个arr 长度为10,根据下标0-9 去遍历字符串,存0-9出现的次数就OK了

public static void main(String[] args) {
    String numStr = "12345678901234567890"; // 20位数
    Set<Character> set = new HashSet<>(); // 存储不重复数字的集合
    for (char ch : numStr.toCharArray()) { // 遍历字符串中的每个数字
        set.add(ch); // 将数字添加到集合中
    }
    if (set.size() == numStr.length()) { // 判断集合中的数字数量是否等于20
        System.out.println("20位数中的数字都是不重复的");
    } else {
        System.out.println("20位数中存在重复的数字");
    }
}