Jessica.j.Karlow
Jessica.j.Karlow

Reputation: 33

Count number of distinct digits in decimal representation of an integer

I am new to C++ development and I was hoping someone could help me with something I have been trying to do.

Say for example I want a function that will, given an integer input, return the number of distinct digits it contains.

So for example, if I have three integers:

int a = 19876;
int b = 25644;
int c = 4444;

If I pass 'a' into the function, I would expect the number 5 to be returned. If 'b' was passed into the function, I would expect '4' to be returned, If 'c' was passed into the function, then 1 would be returned, as they are the number of distinct numbers.

Could someone please illustrate how I could achieve this?

Upvotes: 3

Views: 4595

Answers (5)

bohney
bohney

Reputation: 1205

This could be more concise, but I'm helping you see the way the solution works.

int digitCount(int number) {
    // make an array to store whether you've seen a given digit
    // note that there are 10 elements, one for each digit
    // this will be conveniently indexed 0-9
    bool digitSeen[10];

    // set each seen digit
    int count = 0;
    while (number != 0) {
        // get the rightmost digit with the modulo operator (%)
        int digit = number % 10;
        if (digitSeen[digit] == false) {
            // only count if this is the first time we have seen it
            ++count;
            digitSeen[digit] = true;
        }
        // pop off the right-most digit by dividing by 10
        number /= 10;
    }

    return count;
}

Upvotes: 4

P.P
P.P

Reputation: 121387

Using the mod operator and you can count it:

int distinct(int a)
{
    int ele[10]={0};

    if(a==0) return 1;
    if(a<0) a=a*-1;

    while(a)
    {
        int t=a%10;
        ele[t]=1;
        a=a/10;
    }

    for (i=0;i<10;i++)
        if (ele[i])
            count++;

    return count;
}

This will work only for both positive numbers and negative numbers.

Upvotes: 5

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153820

You mean you want to find the number of different decimal digit in the integer?

int distinct_digits(int value) {
    std::ostringstream out;
    out << value;
    std::string digits = out.str();
    std::sort(digits.begin(), digits.end());
    return std::unique(digits.begin(), digits.end()) - digits.begin();
}

(not compiled or tested but the basic idea should work)

Upvotes: 6

iruel
iruel

Reputation: 11

if you mean to return a floating point to get a decimal just return it as a float and the compiler should to an implicit type conversion. this is not generally good code but it works. a better way might be to hand the value to a temporary float like

float a_float = a;
return a_float;

Upvotes: -1

Puppy
Puppy

Reputation: 146910

You can compute the distinct number thing just fine, but there's no way to go from 'a' to the value of the variable a;. You can hardcode it- but that's fairly maintenance-heavy.

Upvotes: 1

Related Questions