# Check for repeated digits!

• 11-07-2003
CrackerJack
Check for repeated digits!
OK this code works to display one of the numbers that would be repeated. However I need to display all the numbers that may be repeated. Would I need to incorporate a printf function somewhere in the loop?

Code:

```#include <stdio.h> #define TRUE 1 #define FALSE 0 typedef int Bool; main () {         Bool digit_seen[10] = {0};         int digit = 0;         int display_digit = 0;         long int n = 0;         printf("Enter a number: ");         scanf("%ld", &n);         while (n > 0)         {                 digit = n % 10;                 if (digit_seen[digit])                 {                         display_digit = digit;                         break;                 }                 digit_seen[digit] = TRUE;                 n /= 10;         }         if (n > 0)                 printf ("Digits repeated are: %ld\n\n", display_digit);         else                 printf("No repeated digit\n\n");         return 0; }```
• 11-07-2003
Hammer
>>Would I need to incorporate a printf function somewhere in the loop?
Yes, that would be one way to do it.
• 11-07-2003
The Dog
Instead of having a boolean array, have an integer array, then for each time a digit is encountered, just increment that integer in the array.

eg.
Code:

```int digit_seen[10] = {0}; int digit, n; printf("Enter a number: "); scanf("%ld", &n); while (n > 0) {     digit = n % 10;     digit_seen[digit]++;     n /= 10; }```
This way, you can do the printing after the loop.
• 11-08-2003
CrackerJack
Quote:

Originally posted by The Dog
Instead of having a boolean array, have an integer array, then for each time a digit is encountered, just increment that integer in the array.

eg.
Code:

```int digit_seen[10] = {0}; int digit, n; printf("Enter a number: "); scanf("%ld", &n); while (n > 0) {     digit = n % 10;     digit_seen[digit]++;     n /= 10; }```
This way, you can do the printing after the loop.

OK...I can see how you changed it. However, it still gives out only one printout. If there are more repeated numbers then they are over written. Is there suppose to be something in the loop to catch them for a check?

CJ
• 11-08-2003
Hammer
>> Is there suppose to be something in the loop to catch them for a check?<<
The digit_seen array holds a count of how many times each digit has been seen. Simply loop through this array, printing all entries with a count greater than 1.
• 11-08-2003
CrackerJack
Thank you guys!! You have no idea how much you helped me out! That last phrase by hammer (even though a DUH) made me realize a concept of arrays that I was missing.

CJ