Count the Number of Unique Digits in an Integer using Java – 使用 Java 计算整数中唯一数字的个数

最后修改: 2024年 3月 5日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll explore how to count the number of unique digits in an integer using Java.

在这个简短的教程中,我们将探讨如何使用 Java 计算整数中的唯一数字个数。

2. Understanding the Problem

2.了解问题

Given an integer, our goal is to count how many unique digits it contains. For example, the integer 567890 has six unique digits, while 115577 has only three unique digits (1, 5, and 7).

给定一个整数,我们的目标是计算它包含多少个唯一的数字。例如,整数 567890 有 6 个唯一数字,而 115577 只有 3 个唯一数字(1、5 和 7)。

3. Using a Set

3.使用集</em

The most straightforward way to find the number of unique digits in an integer is by using a Set. Sets inherently eliminate duplicates, which makes them perfect for our use case:

查找整数中唯一数字个数的最直接方法是使用 Set 。集合从本质上消除了重复,因此非常适合我们的用例:

public static int countWithSet(int number) {
    number = Math.abs(number);
    Set<Character> uniqueDigits = new HashSet<>();
    String numberStr = String.valueOf(number);
    for (char digit : numberStr.toCharArray()) {
        uniqueDigits.add(digit);
    }
    return uniqueDigits.size();
}

Let’s break down our algorithm’s steps:

让我们来分解一下算法的步骤:

  • Convert the integer into a string to easily iterate over each digit.
  • Iterate through each character of the string and add to a HashSet.
  • The size of the HashSet after iteration gives us the count of unique digits.

The time complexity of this solution is O(n), where n is the number of digits in the integer. Adding to a HashSet and checking its size are both O(1) operations, but we still have to iterate through each digit.

此解决方案的时间复杂度为 O(n),其中 n 是整数的位数。向 HashSet 添加和检查其大小都是 O(1) 操作,但我们仍需遍历每个数字。

4. Using Stream API

4.使用流应用程序接口

Java’s Stream API provides a concise and modern solution to count the number of unique digits in an integer. This method leverages the power of streams to process sequences of elements, including distinct elements, in a collection-like manner:

Java 的 Stream API 为计算整数中唯一数字的个数提供了简洁而现代的解决方案。该方法利用流的强大功能,以类似于集合的方式处理元素序列,包括不同的元素:

public static long countWithStreamApi(int number) {
    return String.valueOf(Math.abs(number)).chars().distinct().count();
}

Let’s examine the steps involved:

让我们来看看其中的步骤:

  • Convert the number to a string.
  • Obtain a stream of characters by using the chars() method from the string.
  • Use the distinct() method to filter out duplicate digits.
  • Use the count() method to get the number of unique digits.

The time complexity is the same as the first solution.

时间复杂度与第一种解决方案相同。

5. Using Bit Manipulation

5.使用位操作

Let’s explore one more solution. Bit manipulation also offers a way to track unique digits:

让我们再探索一种解决方案。位操作也提供了一种追踪唯一数字的方法:

public static int countWithBitManipulation(int number) {
    if (number == 0) {
        return 1;
    }
    number = Math.abs(number);
    int mask = 0;
    while (number > 0) {
        int digit = number % 10;
        mask |= 1 << digit;
        number /= 10;
    }
    return Integer.bitCount(mask);
}

Here are the steps of our code this time:

下面是我们这次的代码步骤:

  • Initialize an integer mask to 0. Each bit in mask will represent a digit from 0-9.
  • Iterate through each digit of the number.
  • For each digit, create a bit representation. If the digit is d, then the bit representation is 1 << d.
  • Use bitwise OR to update mask. This marks the digit as seen.
  • Count the number of bits set to 1 in mask. This count is the number of unique digits.

The time complexity is also the same as the above solutions.

时间复杂度也与上述解决方案相同。

6. Conclusion

6.结论

This article provided different ways to count the number of unique digits in an integer, along with their time complexities.

本文提供了计算整数中唯一数字个数的不同方法及其时间复杂性。

The example code from this article can be found over on GitHub.

本文的示例代码可在 GitHub 上找到