# Thread: find duplicate digit's in array

1. ## find duplicate digit's in array

I am trying to write c program to find duplicate digit's in array

something look's like this
Digits = [ 1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5 ]

Digit 1 display 3 times
Digit 2 display 2 times
Digit 3 display 3 times
Digit 4 display 1 time
Digit 5 display 2 time

Here is my attempt

Code:
``` #include<stdio.h>

#include<stdlib.h>

int main()
{
int i = 0;

int = Digits[] = {1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5 }

for ( i = 0; i < 11; i++)
{

}
return 0;
}```

2. Looks like you want to print how many times a value repeats consecutively.
That requires counting values that are the same until you reach a different value or the end of the array.
When that happens, you need to print the count and then reset it to 1 (you've seen one so far of a new value).

3. Originally Posted by john.c
Looks like you want to print how many times a value repeats consecutively.
That requires counting values that are the same until you reach a different value or the end of the array.
When that happens, you need to print the count and then reset it to 1 (you've seen one so far of a new value).
I need to count as long as the value of digit remain same. I don't understand how to implement this in programming

4. Originally Posted by Rahul11
I need to count as long as the value of digit remain same. I don't understand how to implement this in programming
One approach is to keep track of the current value with a variable. Another approach is to compare the value of the current element with the value of the previous element.

5. Originally Posted by laserlight
Another approach is to compare the value of the current element with the value of the previous element.
How to do with second approach ?

previous = 1
current = 1

bothe are same count1 = 1

previous = 1
current = 1

bothe are same count1 = 2

previous = 1
current = 1

bothe are same count1 = 3

previous = 1
current = 2

bothe are same : No

6. Why don't you give it a try? In the code you showed, you didn't even have a variable for the count. Put in a bit more effort.

7. Originally Posted by laserlight
Why don't you give it a try? In the code you showed, you didn't even have a variable for the count. Put in a bit more effort.
Code:
``` #include<stdio.h>

int main ()
{
int Digits[ ] = {1, 1, 1, 2, 2, 3, 4, 4, 4};

int current, previous, i;

int count1;
previous = Digits[0];
count1 = 0;

for ( i = 0; i < 9; i++)

{
if ( Digits[i]  == previous)

{
count1++;
}
}

printf(" digit %d repeats %d times ", Digits[i], count1 );

return 0;
}```
digit 1 repeats 3 times

Next digit should be count that are not same value

8. You don't need variables like 'current' and 'previous'. Just compare the current element with the previous one: d[i] == d[i - 1]

Start the loop at the second element and start the count at 1. The count is started at 1 since we're assuming the 1st element (at offset 0) has been counted already. (This may necessitate handling an empty array as a special case before the loop.)

So as long as d[i] == d[i - 1] you can simply ++cnt.
But when that isn't true you need to do two things:
* print out the count of the previous digit (d[i - 1])
* reset count to 1 (you've seen one new digit so far)

You need one extra print after the loop to print the count for the last digit value.

9. Originally Posted by john.c
So as long as d[i] == d[i - 1] you can simply ++cnt.
But when that isn't true you need to do two things:
* print out the count of the previous digit (d[i - 1])
* reset count to 1 (you've seen one new digit so far)

You need one extra print after the loop to print the count for the last digit value.
beyonds this I don't understand how to do it

Code:
``` #include<stdio.h>
int main ()
{
int i = 0;

int Digits[ ] = {1, 1, 1, 1, 1, 3, 4, 4, 4};

int count = 0;

for ( i = 1; i < 9; i++)

{
if ( Digits[ i ] ==  Digits[ i -1 ])
{
count = 1;
}

}

printf(" digit %d repeats %d times ", Digits[i], count );

return 0;
}```
digit 1 repeats 1 times

10. Using an array of counters would be an alternative - works also for unsorted arrays.
Code:
```#include <stdio.h>

#define NUM_DIGITS    9

int main ()
{
int Digits[NUM_DIGITS] = {1, 1, 1, 2, 2, 3, 4, 4, 4};
int Counter[NUM_DIGITS+1]  = {0};

for (int i = 0; i < NUM_DIGITS; i++)
Counter[Digits[i]]++;

printf("Digit Count\n");
printf("-----------\n");
for (int i = 0; i < NUM_DIGITS + 1; i++)
printf("%-5d %d\n", i, Counter[i]);
}```

11. Since int can hold values greater then 9 and smaller then 0, I would do only these little changes to the test code above:
Code:
```#include <stdio.h>
#include <stdlib.h>

#define ARRAY_ELEMENTS(a) ( sizeof (a) / sizeof (a)[0] )

int main( void )
{
// Now, any size is allowed.
static const unsigned int digits[] = { 1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8, 8, 9, 0 };

unsigned int counter[10] = {0};
unsigned int i;

for ( i = 0; i < ARRAY_ELEMENTS(digits); i++ )
{
if ( digits[i] > 9 )
{
fprintf( stderr,
"ERROR: Wrong value at position %u: %u. Only 1 digit values allowed.\n",
i, digits[i] );

return EXIT_FAILURE;
}

counter[digits[i]]++;
}

puts( "Digits count:\n"
"--------------" );
for ( i = 0; i < ARRAY_ELEMENTS( counter ); i++ )
printf( "%u: %u\n", i,  counter[i] );

return EXIT_SUCCESS;
}```

12. Since int can hold values greater then 9 and smaller then 0, I would do only these little changes to the test code above:
Code:
```#include <stdio.h>
#include <stdlib.h>

#define ARRAY_ELEMENTS(a) ( sizeof (a) / sizeof (a)[0] )

int main( void )
{
// Now, any size is allowed.
static const unsigned int digits[] = { 1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8, 8, 9, 0 };

unsigned int counter[10] = {0};
unsigned int i;

for ( i = 0; i < ARRAY_ELEMENTS(digits); i++ )
{
if ( digits[i] > 9 )
{
fprintf( stderr,
"ERROR: Wrong value at position %u: %u. Only 1 digit values allowed.\n",
i, digits[i] );

return EXIT_FAILURE;
}

counter[digits[i]]++;
}

puts( "Digits count:\n"
"--------------" );
for ( i = 0; i < ARRAY_ELEMENTS( counter ); i++ )
printf( "%u: %u\n", i,  counter[i] );

return EXIT_SUCCESS;
}```

13. Good idea.