I have tried to remove the duplicate elements from the array using c language, here I am using 3 for
loops to remove the duplicate elements.
How can this optimize the code?
Are there any other simple methods to remove the duplicate elements?
#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
int main()
{
int arr(MAX_SIZE);
int size;
int i, j, k;
printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr(i));
}
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr(i) == arr(j))
{
for(k=j; k<size; k++)
{
arr(k) = arr(k + 1);
}
size--;
j--;
}
}
}
printf("nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%dt", arr(i));
}
return 0;
}