C Program To Print The Duplicate Elements Of An Array

The initial occurrence of a duplicated element within the array is identified at index 4, which corresponds to the element (2) that is duplicated from index 1. Therefore, the duplicate elements within the provided array are 2, 3, and 8.

ALGORITHM:

  • STEP 1: START
  • STEP 2: INITIALIZE arr= {1, 2, 3, 4, 2, 7, 8, 8, 3}.
  • STEP 3: length = sizeof(arr)/sizeof(arr[0])
  • STEP 4: PRINT "Duplicate elements in given array:"
  • STEP 5: SET i=0. REPEAT STEP 6 to STEP 9 UNTIL i<length
  • STEP 6: SET j=i+1. REPEAT STEP 7 and STEP 8 UNTIL i<length
  • STEP 7: if(arr[i] == arr[j]) PRINT arr[j]
  • STEP 8: j=j+1
  • STEP 9: i+1.
  • STEP 10: RETURN 0.
  • STEP 11: END
  • PROGRAM:

    Example
    
    #include <stdio.h>  
    
       
    
    int main()  
    
    {  
    
        //Initialize array   
    
        int arr[] = {1, 2, 3, 4, 2, 7, 8, 8, 3};   
    
          
    
        //Calculate length of array arr  
    
        int length = sizeof(arr)/sizeof(arr[0]);  
    
          
    
        printf("Duplicate elements in given array: \n");  
    
        //Searches for duplicate element  
    
        for(int i = 0; i < length; i++) {  
    
            for(int j = i + 1; j < length; j++) {  
    
                if(arr[i] == arr[j])  
    
                    printf("%d\n", arr[j]);  
    
            }  
    
        }  
    
        return 0;  
    
    }
    

Output:

Output

Duplicate elements in given array:

2

3

8

Input Required

This code uses input(). Please provide values below: