简体   繁体   中英

To count unique digits in a given number N in java

Given a number N, for example, take 1091, here the count of digits is 4 but the count of unique digits is 3 ie 1, 0 & 9 are unique (since 1 is repeated).

I have tried breaking the number into individual digits and adding it to ArrayList and later converting it to an array. Then, iterating through the array and increasing the count of unique digits by 1 every time I got a unique digit in the array, But I have not got the required output. Kindly someone help in finding the unique digits count in a given number in 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));
    }
}
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;
}

A HashSet stores only unique elements. So, when we return the length of the HashSet after adding each digit of the input integer to it, we can obtain the count of unique digits in that input.

This could be done with a set. A set is a collection of unique elements. Putting all characters (digits) into a set will result in the duplicates being discarded. Its size() will then return the distinct elements.

Using streams:

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

Or leveraging a count on the stream:

String.valueOf(number).chars()
    .distinct()
    .count();
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));
    }
}

For a detailed explanation do check my article on 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM