Programming Examples
C program to remove duplicates from an ordered array
Write a ‘C’ program to remove duplicates from an ordered array. For example, if input array contains 10,10,10,30,40,40,50,80,80,100 then output should be 10,30,40,50,80,100.
Solution
#include <stdio.h>
int main()
{
int arr[100];
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;
}
Output
Enter size of the array : 10
Enter elements in array : 3
4
5
6
6
5
3
7
8
2
Array elements after deleting duplicates : 3 4 5 6 7
8 2