C Program to find duplicate elements in array


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


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

Duplicate elements is: 12 23


Example: How to find duplicate elements in array in c

Run it Now
#include<stdio.h>

int main() {

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

   printf("Enter Size of an Array :");
   scanf("%d", &n);

   printf("\nEnter Array Elements :");
   for (i = 0; i < n; i++) {
       scanf("%d", &arr[i]);
   }

   printf("\nDuplicate 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 :1 5 5 4 5
Duplicate Elements :5