Home / Programs / C Program to Delete duplicate elements from an array
Programming Example

C Program to Delete duplicate elements from an array

👁 1,783 Views
💻 Practical Program
📘 Step by Step Learning
C Program to Delete duplicate elements from an array

Program Code

/* 
C Program to Delete duplicate elements from an array
Author: Atnyla Developer

*/

#include<stdio.h>
 
int main() {
   int arr[20], i, j, k, size;
 
   printf("\nEnter array size : ");
   scanf("%d", &size);
 
   printf("\nAccept Numbers : ");
   for (i = 0; i < size; i++)
      scanf("%d", &arr[i]);
 
   printf("\nArray with Unique list  : ");
   for (i = 0; i < size; i++) {
      for (j = i + 1; j < size;) {
         if (arr[j] == arr[i]) {
            for (k = j; k < size; k++) {
               arr[k] = arr[k + 1];
            }
            size--;
         } else
            j++;
      }
   }
 
   for (i = 0; i &lt; size; i++) {
      printf("%d ", arr[i]);
   }
 
   return (0);
}

Output


Enter array size : 5

Accept Numbers : 1
2
2
3
4

Array with Unique list  : 1 2 3 4 

Explanation

C Program to Delete duplicate elements from an array

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.