Thread: find duplicate digit's in array

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    66

    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. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,390
    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).
    A long time ago being crazy meant something. - Charles Manson

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by john.c View Post
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,410
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by laserlight View Post
    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. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,410
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by laserlight View Post
    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. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,390
    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.
    A long time ago being crazy meant something. - Charles Manson

  9. #9
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by john.c View Post
    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. #10
    Registered User
    Join Date
    Sep 2020
    Posts
    146
    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. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    967
    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. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    967
    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. #13
    Registered User
    Join Date
    Sep 2020
    Posts
    146
    Good idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find digit place in number
    By vead in forum C Programming
    Replies: 6
    Last Post: 01-28-2018, 02:36 AM
  2. Find duplicate number indexes in a vector
    By ///André in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2015, 02:07 PM
  3. Help: Find a integer digit in a string
    By Vivasso in forum C Programming
    Replies: 8
    Last Post: 11-06-2010, 05:50 PM
  4. need to find duplicate strings in file`
    By e.vikaas in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 10:45 AM
  5. find and replace duplicate numbers in array
    By Cathalo in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2009, 11:05 AM

Tags for this Thread