# Counting the Number of Unique Digits in an Integer in Java

Counting the number of unique digits in an integer is an interesting problem that involves analyzing the individual digits of a number and determining how many of them are distinct. This article will explore several approaches to solving this problem using Java.

## 1. Understanding the Problem

To solve the problem of counting unique digits in an `int`

like `229987`

or `627765`

, we aim to identify and tally the distinct digits present in each number.

#### Example:

Consider the integer `229987`

:

- Digits: 2, 2, 9, 9, 8, 7
- Unique Digits: 2, 9, 8, 7 (each digit appears only once)

The count of unique digits in `229987`

is 4.

Similarly, for the integer `627765`

:

- Digits: 6, 2, 7, 7, 6, 5
- Unique Digits: 6, 2, 7, 5 (each digit appears only once)

The count of unique digits in `627765`

is 4.

## 2. Using a Set

One of the straightforward ways to solve this problem is by leveraging a `Set`

data structure to keep track of unique digits encountered.

public class UniqueDigitsCounter { public static int countUniqueDigits(int number) { // Take the absolute value of the number number = Math.abs(number); Set<Integer> digits = new HashSet<>(); while (number > 0) { int digit = number % 10; digits.add(digit); number /= 10; } return digits.size(); } public static void main(String[] args) { int number = 229987; int uniqueDigitsCount = countUniqueDigits(number); System.out.println("Number of unique digits: " + uniqueDigitsCount); } }

In this code snippet, we use `Math.abs(number)`

to ensure that `number`

is positive before processing its digits. we iterate through each digit of the number by repeatedly taking the remainder when divided by 10 (`number % 10`

). We add each digit to a `Set`

which automatically handles uniqueness. Finally, the size of the `Set`

gives us the count of unique digits.

The output from running this code is:

## 3. Using Bit Manipulation

Bit manipulation can also be used to track unique digits. Below is an example:

public class BitsManipulationUniqueDigitsCounter { public static int countUniqueDigits(int number) { // Take the absolute value of the number number = Math.abs(number); int seen = 0; while (number > 0) { int digit = number % 10; seen |= (1 << digit); number /= 10; } int count = 0; while (seen > 0) { if ((seen & 1) == 1) { count++; } seen >>= 1; } return count; } public static void main(String[] args) { int number = 627765; int uniqueDigitsCount = countUniqueDigits(number); System.out.println("Number of unique digits: " + uniqueDigitsCount); } }

In this method, we use an integer as a bitmask to represent which digits (from 0 to 9) have been seen. As we iterate through each digit of the number, we set the corresponding bit in the bitmask. Finally, we count the number of bits that are set (indicating the unique digits).

Output from running the above code snippet is:

Number of unique digits: 4

## 4. Using Stream API

We can efficiently calculate the count of unique digits in any given integer `number`

by leveraging Java’s Stream API operations (`chars()`

, `distinct()`

, `count()`

) on the string representation of the number like this:

public class StreamsUniqueDigitsCounter { public static int countUniqueDigits(int number) { // Take the absolute value of the number number = Math.abs(number); long digits = String.valueOf(number).chars().distinct().count(); return (int) digits; // Using Java 8+ // return Math.toIntExact(digits); } public static void main(String[] args) { int number = 62776; int uniqueDigitsCount = countUniqueDigits(number); System.out.println("Number of unique digits: " + uniqueDigitsCount); } }

In this code snippet, we use the `String.valueOf(number)`

method to convert the integer `number`

into its string representation. After converting the number to a string, the `chars()`

method is invoked on the string which returns an `IntStream`

of Unicode code points representing the characters of the string. We then use the `distinct()`

method to filter out any duplicate characters. Finally, we use the `count()`

method to count the number of distinct digits present in the original number.

The output is:

Number of unique digits: 3

## 5. Download the Source Code

This article has explored different ways to tackle the problem of counting unique digits in an integer using Java. This was an article on how to count the unique digits in an int value using Java.

**Download**

You can download the full source code of this example here:

**Count unique digits in int using Java**