Thread: Program to find repeated number in array

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    Program to find repeated number in array

    Logic to find repeated number in array

    compare array element

    array[0] == array[1]
    array[0] == array[2]
    array[0] == array[3]
    array[0] == array[4]


    array[1] == array[0]
    array[1] == array[2]
    array[1] == array[3]
    array[1] == array[4]


    array[2] == array[0]
    array[2] == array[1]
    array[2] == array[3]
    array[2] == array[4]


    array[3] == array[0]
    array[3] == array[1]
    array[3] == array[2]
    array[3] == array[4]


    array[4] == array[0]
    array[4] == array[1]
    array[4] == array[2]
    array[4] == array[3]

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
        int i, array[5] = {1, 2, 3, 4, 4};
        
        int duplicate = array[0];
    
    
         for ( i = 1; i <5; i++)
             
             { 
               if (duplicate ==array[i]
               {
                   printf("%d ", duplicate);
               }
             }
                
        return 0;
    }
    
    How to check all conditions

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps make your code follow your logic, which suggests you need two nested loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Perhaps make your code follow your logic, which suggests you need two nested loops.
    I'm trying to implement logic. I understand nested loops.

    Code:
    #include <stdio.h>
    
    int main(void)
     {
            
            int i, j;
            
            for( i = 0; i < 5; i++)
            {
               printf(" i = %d \n", i);
            
                      for( j = 0; j < 5; j++)
                           {
                                 printf(" j = %d \n",j);  
                            }           
               
            }
            
            return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I'm sure you'll figure it out if you also print array[i] and array[j]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    I'm sure you'll figure it out if you also print array[i] and array[j]
    What's wrong here

    Code:
    #include <stdio.h> 
    int main(void)
     {
             
            int i, j;
            int array[5] = {1, 2, 3, 4, 4};
             
            for( i = 0; i < 5; i++)
            {
             
                      for( j = 0; j < 5; j++)
                           {
                                if (array[j] ==array[j+1])
                                {
                                    printf("%d", array[j] );
                                     break;
                                }
                            }           
                
            }
             
            return 0;
    }
    Last edited by vajra11; 08-27-2018 at 01:19 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > What's wrong here
    Did you do as I suggested, or did you lunge straight for the problem without understanding intermediate steps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    > What's wrong here
    Did you do as I suggested, or did you lunge straight for the problem without understanding intermediate steps?
    When I run that program it find repeated number but it print that number five times like 44444

    I just want to find repeat number and that is 4

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So what happened to the "int duplicate = array[0];" idea, except you now make it int duplicate = array[i];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    So what happened to the "int duplicate = array[0];" idea, except you now make it int duplicate = array[i];
    This program find repeated number

    Code:
    #include <stdio.h> 
    
    int main(void)
     {
             
            int i, j;
    		
            int array[5] = {1, 1, 3, 4, 4};
             
            for( i = 0; i < 5; i++)
            {
             
                      for( j = i+1; j < 5; j++)
    					  
    					    if (array[i] == array[j])
                                {
    								printf("%d ", array[i] );																
    							}							  
            }
           
            return 0;
    }

  10. #10
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    I guess you should break out of the loop as soon as it finds the repeated number
    Code:
    if(array[i] == array[j])
    {
        printf("Found repeated number %d\n\n", array[i]);
        break;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to find maximum number in array
    By vead in forum C Programming
    Replies: 8
    Last Post: 12-18-2017, 11:44 AM
  2. Replies: 6
    Last Post: 11-11-2017, 01:26 AM
  3. Replies: 8
    Last Post: 12-21-2012, 11:48 PM
  4. Find no of times a no is repeated in a array.
    By Anitrex in forum C Programming
    Replies: 4
    Last Post: 03-18-2012, 06:07 AM
  5. Replies: 25
    Last Post: 10-31-2009, 01:45 AM

Tags for this Thread