Advertisement

C Program to find duplicate elements in the array


In this program, You will learn how to find duplicate elements in the array in c.


List is: 1, 2, 12, 23, 12, 23

Duplicate elements: 12 23

Example: How to find duplicate elements in the array in c

#include<stdio.h>
int main() {

   int i, j, n, v = 1;
   int arr[100];

   printf("Enter size of an array:");
   scanf("%d", &n);

   printf("Enter array elements:");
   for (i = 0; i < n; i++) {
       scanf("%d", &arr[i]);
   }

   printf("Duplicate elements:");

   for (i = 0; i < n; i++) {
       for (j = i + 1; j < n; j++) {
           if (arr[i] == arr[j]) {
               if (v == 1 && arr[j] != '\0') {
                   printf("%d ", arr[i]);
               }
               arr[j] = '\0';
               v++;
           }
       }
       v = 1;
   }

   return 0;
}

Output:

Enter size of an array:5                                                                                                               
Enter array elements:10 20 10 30 30                                                                                                    
Duplicate elements:10 30