Count unique digits in a given number How do I find the number of unique digits in a given number. 

e.g
if 'input1' is 252 it must return 1, since 5 is the non-repeating digit;
if 'input1' is 25000 it must return 2, since digits 2 and 5 are the only non-repeating ones.

Is there a better way to solve this problem?

// COUNT UNIQUE DIGITS

int nonRepeatDigitsCount(int input1){
int i=0,j,k;
int n=input1;
int count=0,check,c2=0;

// FINDING NUMBER OF DIGITS ON THE GIVEN NUMBER
while(n&gt;0){

    n=n/10;
    c2++;
    }
//CREATING ARRAY OF THE 'c2' SIZE       
int arr[c2];
n=input1;
//STORING INDIVIDUAL DIGITS IN THE ARRAY
while(n&gt;0)
{
    arr[i]=n%10;
    n=n/10;
    i++;

}
// CONDITION TO FIND NON REPEATING DIGITS
for(j=0;j&lt;c2;j++)
{
    check=0;

    for(k=0;k&lt;c2;k++)
    {
          if(j!=k)
          {
           if(arr[j]==arr[k])
           {
            check++;
           }
          } 

    }

    if(check==0)
     {
        count++;
     }
 }

return count;
}
This is needlessly complicated and inefficient. Rather than making an array of all digits in the array, you should make an array of counted occurrences. Since a digit can have a value 0 to 9, this array size will be 10. For each occurrence, increase the value of the index in the array corresponding to the digit by 1.

This also means that you can count non-repeating characters on the fly while iterating through the number. When the number of occurrences is exactly 1, it should be counted. If more than that, it shouldn't.

This makes the code behave the same no matter the number of digits used as input.

Example:

#include &lt;stdio.h&gt;

int non_repeating (int val)
{
  if(val == 0) // special case, value 0 gives 1 digit
  {
    return 1;
  }

  int digit_count[10]={0};
  int non_rep=0;

  for(; val!=0; val/=10)
  {
    int i = val%10;
    digit_count[i]++;

    if(digit_count[i]==1)
    {
      non_rep++;
    }
    else if(digit_count[i]==2)
    {
      non_rep--;
    }
  }

  return non_rep;
}

int main (void)
{
  printf("%d %d\n", 252,        non_repeating(252));
  printf("%d %d\n", 25000,      non_repeating(25000));
  printf("%d %d\n", 1234567890, non_repeating(1234567890));
  printf("%d %d\n", 0,          non_repeating(0));
}


Output:

252 1
25000 2
1234567890 10
0 1