-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveduplicates.c
95 lines (89 loc) · 1.85 KB
/
removeduplicates.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//Removing duplicates | Vishruth codes
#include<stdio.h>
#include<stdlib.h>
//Defining a node
struct node
{
int data;
struct node *link;
};
struct node *root=NULL;
//append function for linked list
int append(int n)
{
int count=1;
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=n;
temp->link=NULL;
if(root==NULL) //list is empty
{
root=temp;
}
else
{
struct node *p;
p=root;
while(p->link!=NULL)
{
p=p->link;
count++;
}
p->link=temp;
}
return count;
}
//Function to determine whether the element is in the L.L or not
int search(int n)
{
int count=0;
struct node *p;
p=root;
while(p!=NULL)
{
if(p->data==n)
{
count++;
}
p=p->link;
}
return count;
}
//Function which compares the array element with the elements of the L.L
//Onlu unique elements are inserted into the L.L. Duplicates aren't allowed.
//Uses search() function to determine whether unique or duplicate.
void removingduplicates(int ar[], int n)
{
int i=1, len;
len=append(ar[0]);
do
{
if(search(ar[i])==0)
{
append(ar[i]);
}
i++;
}while(i<n);
}
//To display the linked list
void display()
{
struct node *p;
p=root;
while(p!=NULL)
{
printf("%d, ",p->data);
p=p->link;
}
}
int main()
{
//Hardcoding the array.
int ar[]={2,4,5,5,7,9,11,14,17,17,17,20};
printf("\nThe array: 2,4,5,5,7,9,11,14,17,17,17,20");
int n=sizeof(ar)/sizeof(ar[0]);
removingduplicates(ar,n);
printf("\nThe array without duplicates: ");
display();
return 0;
}