Write a C function with 1-D array and its size as arguments, which is called by main() function and remove the duplicate elements from an array also display the resultant array? ( this is the question )
Code:
#include <stdio.h>
#define MAX_SIZE 100
int duplicate_elements(int arr[], int n);
int main()
{
int arr[MAX_SIZE];
int n;
int i, j, k;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements for the array : ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
duplicate_elements(arr,n);
printf("\nArray elements after deleting duplicates is : ");
for(i=0; i<n; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
int duplicate_elements(int arr[], int n)
{
int i,k,j;
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr[i] == arr[j])
{
for(k=j; k<n; k++)
{
arr[k] = arr[k + 1];
}
n--;
j--;
}
}
}
}