Click here to Skip to main content
15,888,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For this function, I cannot use arrays or strings.

Example: Given the integer 4321432143, the function would output 4, since there are 4 different digits in the number.

12345 would return 5, 123 would return 3, etc.

What I have tried:

C
void calcUnique(long long num)
{
  int unique; //number of different digits in given number   
  int digit
  long long value; 
  int seen;
  int count; 

  value = num;
  seen = 0;
  unique = 0;
  count = 0;
 
 do
  {
    digit = value % 10;
    if((seen & (1 << digit)) == 0)
    {
      ++count;
      seen = seen | (1 << digit);
    }
    value = value / 10;
  }while(value > 0);

  printf("Total Unique Digits: %d\n", unique);
  return;
}
Posted
Updated 24-Mar-16 14:01pm
v2

You're already on a good way. But you never set unique to any other value than 0 before you display it. And count isn't being used for anything except increasing it in that if-statement......

(multiple dots on purpose)
 
Share this answer
 
v2
Comments
Member 12413627 24-Mar-16 18:08pm    
Ah can't believed i missed incrementing unique! Thank you!
Sascha Lefèvre 24-Mar-16 18:10pm    
You're welcome! :)
Matt T Heffron 24-Mar-16 18:29pm    
It looks like you can just use count instead of unique.
Sergey Alexandrovich Kryukov 24-Mar-16 19:37pm    
5ed.
—SA
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Give it a try!
The debugger is a great tool to understand what is wrong.
Don't forget to explain what is wrong when you ask a question.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900