【问题标题】:To count unique digits in a given number N in java在java中计算给定数字N中的唯一数字
【发布时间】:2020-09-20 23:28:24
【问题描述】:

给定一个数字 N,例如取 1091,这里的位数是 4,但唯一位数是 3,即 1、0 和 9 是唯一的(因为 1 是重复的)。

我尝试将数字分解为单个数字并将其添加到 ArrayList 中,然后将其转换为数组。然后,每次我在数组中得到一个唯一数字时,遍历数组并将唯一数字的计数增加 1,但我没有得到所需的输出。请有人帮助在 Java 中查找给定数字中的唯一数字计数。

import java.util.ArrayList;

public class UniqueDigits {
    static int uniqueDigitCount(int n) {
        ArrayList<Integer> ar = new ArrayList<>();
        int temp = n;
        int count = 1;
        do {
            ar.add(temp % 10);
            temp /= 10;
        }
        while (temp > 0);
        Integer[] arr = new Integer[ar.size()];
        arr = ar.toArray(arr);
        if (arr.length > 0) {
            for (int i = 0; i < arr.length - 1; i++) {
                if (arr[i] != arr[i + 1]) {

                    count++;
                }
            }
            return count;
        } else {
            return 0;
        }
    }

    public static void main(String[] args) {
        System.out.println(uniqueDigitCount(1091));
    }
}

【问题讨论】:

  • 请贴出代码。
  • 您可以将它们添加到 HashSet 中,而不是将数字添加到 ArrayList 中,它的大小实际上会为您提供唯一数字的数量。
  • @akuzminykh 是的,我会发布我的代码
  • @KonstantinYovkov 我会试试的
  • @KonstantinYovkov 谢谢,使用 HashSet 可以完美地找到 uniqueDigitCount。我在答案部分添加代码。

标签: java unique counting digits


【解决方案1】:
static int uniqueDigitCount(int n) {
    HashSet<Integer> hs = new HashSet<Integer>();
    int count;
    if(n == 0)
    {
        count = 1;
    }
    else
    {
        while(n > 0)
        {
            hs.add(n % 10);
            n /= 10;
        }
        count = hs.size();
    }
    
    return count;
}

HashSet 只存储唯一元素。因此,当我们在将输入整数的每个数字添加到它之后返回 HashSet 的长度时,我们可以获得该输入中唯一数字的计数。

【讨论】:

  • 请注意,对于 N = 0,您的函数返回 0,但它应该返回 1
【解决方案2】:

这可以通过一组来完成。集合是唯一元素的集合。将所有字符(数字)放入一个集合将导致重复项被丢弃。然后它的size() 将返回不同的元素。

使用流:

int number = 1091;
long uniques = String.valueOf(number).chars()
    .mapToObj(c -> c)
    .collect(Collectors.toSet())
    .size();

或利用流的计数:

String.valueOf(number).chars()
    .distinct()
    .count();

【讨论】:

    【解决方案3】:
    import java.util.ArrayList;
    import java.util.HashSet;
    
    public class UniqueDigits {
    
        static int uniqueDigitCount(int n) {
            ArrayList<Integer> ar = new ArrayList<>();
            int temp = n;
            int count = 1;
    
            do {
                ar.add(temp % 10);
                temp /= 10;
            } while (temp > 0);
            Integer[] arr = new Integer[ar.size()];
            arr = ar.toArray(arr);
            HashSet<Integer> hs = new HashSet<Integer>();
    
            for (int i = 0; i < arr.length - 1; i++) {
                hs.add(arr[i]);
            }
            return hs.size();
        }
    
        public static void main(String[] args) {
            System.out.println(uniqueDigitCount(1091));
        }
    }
    

    【讨论】:

    • 有些不对劲。对于 N = 1092,此算法给出 3,但它应该是 4。
    【解决方案4】:

    有关详细说明,请查看我关于 gfg (Count of unique digits in a given number N) 的文章:

    import java.util.*;
     
    class UniqueDigits {
     
        // Function that returns the count
        // of unique digits of number N
        public static void
        countUniqueDigits(int N)
        {
            // Initialize a variable to
            // store count of unique digits
            int res = 0;
     
            // Initialize cnt array to
            // store digit count
            int cnt[] = { 0, 0, 0, 0, 0,
                          0, 0, 0, 0, 0 };
     
            // Iterate through digits of N
            while (N > 0) {
     
                // Retrieve the last
                // digit of N
                int rem = N % 10;
     
                // Increase the count
                // of the last digit
                cnt[rem]++;
     
                // Remove the last
                // digit of N
                N = N / 10;
            }
     
            // Iterate through the
            // cnt array
            for (int i = 0;
                 i < cnt.length; i++) {
     
                // If frequency of
                // digit is 1
                if (cnt[i] == 1) {
     
                    // Increment the count
                    // of unique digits
                    res++;
                }
            }
     
            // Return the count of unique digit
            System.out.println(res);
        }
     
        public static void main(String[] args)
        {
            // Given Number N
            int N = 2234262;
     
            // Function Call
            countUniqueDigits(N);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-08
      • 2013-02-15
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多