-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function to delete all duplicate elements from an array.c
77 lines (77 loc) · 2.06 KB
/
Function to delete all duplicate elements from an array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*Function to delete all duplicate elements from an array*/
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create(int *,int *);
void display(int *,int);
void deldup(int *,int *);
void main()
{
int a[MAX],n=0;
create(a,&n);
display(a,n);
deldup(a,&n);
display(a,n);
getch();
}
void create(int *a,int *n)
{
int ele;
while(1)
{
if(*n==MAX)
{
printf("Array is full,insertion not possible.");
break;
}
else
{
printf("\nEnter elements(-1 to exit):");
scanf("%d",&ele);
if(ele==-1)
break;
else
a[(*n)++]=ele;
}
}
}
void display(int *a,int n)
{
int i;
if(n==0)
printf("\nArray is empty.");
else
{
printf("\nElement of array are:-");
printf("\nPosition element");
for(i=0;i<n;i++)
printf("\na[%d]\t\t=%d",i,a[i]);
}
}
void deldup(int *a,int *n)
{
int i,j,k,count=0;
if(*n==0)
printf("\nList is empty,Deletion not possible.");
else
{
for(i=0;i<(*n)-1;i++)
{
j=i+1;
while(j<*n)
if(a[i]==a[j])
{
count++;
for(k=j;k<(*n)-1;k++)
a[k]=a[k+1];
(*n)--;
}
else
j++;
}
if(count==0)
printf("\nNo duplicate element found.");
else
printf("\n%d duplicate element deleted.",count);
}
}