Count of unique digits in a given number N
Last Updated :
16 Dec, 2023
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: 1
Explanation:
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.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countUniqueDigits( int N)
{
int res = 0;
int cnt[10] = { 0 };
while (N > 0) {
int rem = N % 10;
cnt[rem]++;
N = N / 10;
}
for ( int i = 0; i < 10; i++) {
if (cnt[i] == 1) {
res++;
}
}
return res;
}
int main()
{
int N = 2234262;
cout << countUniqueDigits(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void
countUniqueDigits( int N)
{
int res = 0 ;
int cnt[] = { 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 };
while (N > 0 ) {
int rem = N % 10 ;
cnt[rem]++;
N = N / 10 ;
}
for ( int i = 0 ;
i < cnt.length; i++) {
if (cnt[i] == 1 ) {
res++;
}
}
System.out.println(res);
}
public static void main(String[] args)
{
int N = 2234262 ;
countUniqueDigits(N);
}
}
|
Python3
def countUniqueDigits(N):
res = 0
cnt = [ 0 ] * 10
while (N > 0 ):
rem = N % 10
cnt[rem] + = 1
N = N / / 10
for i in range ( 10 ):
if (cnt[i] = = 1 ):
res + = 1
return res
N = 2234262
print (countUniqueDigits(N))
|
C#
using System;
class GFG{
public static void countUniqueDigits( int N)
{
int res = 0;
int [] cnt = { 0, 0, 0, 0, 0,
0, 0, 0, 0, 0 };
while (N > 0)
{
int rem = N % 10;
cnt[rem]++;
N = N / 10;
}
for ( int i = 0; i < cnt.Length; i++)
{
if (cnt[i] == 1)
{
res++;
}
}
Console.WriteLine(res);
}
public static void Main(String[] args)
{
int N = 2234262;
countUniqueDigits(N);
}
}
|
Javascript
<script>
function countUniqueDigits(N)
{
let res = 0;
let cnt = [0, 0, 0, 0, 0,
0, 0, 0, 0, 0];
while (N > 0) {
let rem = N % 10;
cnt[rem]++;
N = Math.floor(N / 10);
}
for (let i = 0; i < 10; i++) {
if (cnt[i] == 1) {
res++;
}
}
return res;
}
let N = 2234262;
document.write(countUniqueDigits(N));
</script>
|
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.
Share your thoughts in the comments
Please Login to comment...