Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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

2. Understanding the Problem

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).

3. Using a Set

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:

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.

4. Using Stream API

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:

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

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

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.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.