Count of unique digits in a given number N - GeeksforGeeks

01 Mar, 2021
Given a number N, the task is to count the number of unique digits in the given number.
Examples:
Input: N = 22342 Output: 2 Explanation:The digits 3 and 4 occurs only once. Hence, the output is 2.
Input: N = 99677 Output: 1Explanation:The digit 6 occurs only once. Hence, the output is 1. 
Naive Approach: By this approach, the problem can be solved using two nested loops. In the first loop, traverse from the first digit of the number to the last, one by one. Then for each digit in the first loop, run a second loop and search if this digit is present anywhere else as well in the number. If no, then increase the required count by 1. In the end, print the calculated required count.Time Complexity: O(L2) Auxiliary Space: O(1) 
Efficient Approach: The idea is to use Hashing to store the frequency of the digits and then count the digits with a frequency equal to 1. Follow the steps below to solve the problem: 
Create a HashTable of size 10 for digits 0-9. Initially store each index as 0.Now for each digit of number N, increment the count of that index in the hashtable.Traverse the hashtable and count the indices that have value equal to 1.At the end, print/return this count.
Create a HashTable of size 10 for digits 0-9. Initially store each index as 0.
Now for each digit of number N, increment the count of that index in the hashtable.
Traverse the hashtable and count the indices that have value equal to 1.
At the end, print/return this count.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <iostream>using namespace std; // Function that returns the count of// unique digits of the given numberint countUniqueDigits(int N){    // Initialize a variable to store    // count of unique digits    int res = 0;     // Initialize cnt array to store    // digit count    int cnt[10] = { 0 };     // Iterate through the 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 < 10; i++) {         // If frequency of        // digit is 1        if (cnt[i] == 1) {             // Increment the count            // of unique digits            res++;        }    }     // Return the count/ of unique digit    return res;} // Driver Codeint main(){    // Given array arr[]    int N = 2234262;     // Function Call    cout << countUniqueDigits(N);    return 0;}
// Java program for the above approachimport java.util.*; class GFG {     // 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);    }     // Driver Code    public static void main(String[] args)    {        // Given Number N        int N = 2234262;         // Function Call        countUniqueDigits(N);    }}
# Python3 program for the above approach # Function that returns the count of# unique digits of the given numberdef countUniqueDigits(N):     # Initialize a variable to store    # count of unique digits    res = 0     # Initialize cnt list to store    # digit count    cnt = [0] * 10     # Iterate through the digits of N    while (N > 0):         # Retrieve the last digit of N        rem = N % 10         # Increase the count        # of the last digit        cnt[rem] += 1         # Remove the last digit of N        N = N // 10     # Iterate through the cnt list    for i in range(10):         # If frequency of        # digit is 1        if (cnt[i] == 1):             # Increment the count            # of unique digits            res += 1     # Return the count of unique digit    return res # Driver Code # Given number NN = 2234262 # Function callprint(countUniqueDigits(N)) # This code is contributed by vishu2908
// C# program for the above approachusing System; class GFG{ // Function that returns the count// of unique digits of number Npublic 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    Console.WriteLine(res);} // Driver Codepublic static void Main(String[] args){         // Given Number N    int N = 2234262;     // Function Call    countUniqueDigits(N);}} // This code is contributed by jrishabh99
<script> // Javascript program for the above approach // Function that returns the count of// unique digits of the given numberfunction countUniqueDigits(N){    // Initialize a variable to store    // count of unique digits    let res = 0;     // Initialize cnt array to store    // digit count    let cnt = [0, 0, 0, 0, 0,                      0, 0, 0, 0, 0];     // Iterate through the digits of N    while (N > 0) {         // Retrieve the last digit of N        let rem = N % 10;         // Increase the count        // of the last digit        cnt[rem]++;         // Remove the last digit of N        N = Math.floor(N / 10);    }     // Iterate through the cnt array    for (let i = 0; i < 10; i++) {         // If frequency of        // digit is 1        if (cnt[i] == 1) {             // Increment the count            // of unique digits            res++;        }    }     // Return the count/ of unique digit    return res;} // Driver Code     // Given array arr[]    let N = 2234262;     // Function Call    document.write(countUniqueDigits(N));     // This code is contributed by Mayank Tyagi </script>
3
Time Complexity: O(N), where N is the number of digits of the number.Auxiliary Space: O(1)
Note: As the used hashtable is of size only 10, therefore its time and space complexity will be near to constant. Hence it is not counted in the above time and auxiliary space. 
jrishabh99
vishu2908
mayanktyagi1709
frequency-counting
number-digits
number-theory
Numbers
Greedy
Hash
Mathematical
School Programming
Hash
number-theory
Greedy
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Huffman Coding | Greedy Algo-3
Fractional Knapsack Problem
Activity Selection Problem | Greedy Algo-1
Coin Change | DP-7
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Internal Working of HashMap in Java
Hashing | Set 1 (Introduction)
Hashing | Set 3 (Open Addressing)
Count pairs with given sum