I have tried to remove the duplicate elements from an array using c language, here I'am using 3 for loop to remove the duplicate elements.
How can this optimize the code?
any other simple method to remove the duplicate elements?
Thanks.
#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("%d\t", arr[i]);
}
return 0;
}