Q. Write a program to remove all duplicate elements from an array.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, j, k, n;
printf("\nEnter array size: ");
scanf("%d", &n);
printf("\nEnter %d array element: ", n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("\nOriginal array is: ");
for(i = 0; i < n; i++)
{
printf(" %d", a[i]);
}
printf("\nNew array is: ");
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; )
{
if(a[j] == a[i])
{
for(k = j; k < n; k++)
{
a[k] = a[k+1];
}
n--;
}
else
{
j++;
}
}
}
for(i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
Output:
Enter array size: 5
Enter 5 array element: 5
7
6
5
7
Original array is: 5 7 6 5 7
New array is: 5 7 6
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, j, k, n;
printf("\nEnter array size: ");
scanf("%d", &n);
printf("\nEnter %d array element: ", n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("\nOriginal array is: ");
for(i = 0; i < n; i++)
{
printf(" %d", a[i]);
}
printf("\nNew array is: ");
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; )
{
if(a[j] == a[i])
{
for(k = j; k < n; k++)
{
a[k] = a[k+1];
}
n--;
}
else
{
j++;
}
}
}
for(i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
Output:
Enter array size: 5
Enter 5 array element: 5
7
6
5
7
Original array is: 5 7 6 5 7
New array is: 5 7 6
0 Comments