Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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 code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1 Comment
Oldest
Newest