Write a function Unique_Occurance and call the function to find the count of unique occurrence digits in a given number N. The number will be passed to the program as the input of type int.
Assumption: The input number will be a positive integer number>=1 and <= 25000.
For e.g.
If the given number is 292, the program should print 1 because there are only 1 unique occurrence digit "9" int this number.
#include <iostream>
using namespace std;
int UniqueDigits(int N);
int main()
{
// Initialize a variable
int N;
cout << "Enter a positive integer number>=1 and <= 25000 : ";
cin >> N;
cout << endl;
if(N >= 1 && N <= 25000)
cout << UniqueDigits(N); // Function Call
return 0;
}
//**********************************************
// Function that returns the count of
// unique digits of the given number
int UniqueDigits(int N)
{
int res = 0; // count of unique digits
int lastDigit = 0; // last digit of N
int i;
// digitArray to store digit count
int digitArray[10] = { 0 };
// loop through the digits of N
while (N > 0) {
lastDigit = N % 10; // Get the last digit N
digitArray[lastDigit]++; // counter of the last digit
N = N / 10; // next last digit
}
// loop through the array digitArray
for ( i = 0; i < 10; i++) {
// If frequency of digit is 1
if (digitArray[i] == 1) {
res++; // count of unique digits
}
}
return res;
}
Comments
Where is the answer?
Leave a comment