-
Notifications
You must be signed in to change notification settings - Fork 0
/
dupliremove.cpp
92 lines (86 loc) · 1.96 KB
/
dupliremove.cpp
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
//Program to delete duplicate from the array by Vishruth Codes
#include <iostream>
using namespace std;
class Arrays
{
public:
int n;
// hardcoding the size of array. 'n' is useless
int arr[5];
// calling constructor to initialize all elements to zero
Arrays()
{
for (int i = 0; i < n; i++)
{
arr[i] = 0;
}
}
// member function to insert elements into the array
void insertion()
{
cout << "\nInsert the elements: ";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
}
// sorting via bubble sort
void bubblesort()
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// member function to remove the duplicates
void removedupli()
{
bubblesort();
int now = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] != now)
{
now = arr[i];
}
else
{
for (int j = i; j < n - 1; j++)
{
arr[j] = arr[j + 1];
}
n--;
i--;
}
}
}
// member function to display the contents of the array
void display()
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << ", ";
}
}
};
int main()
{
Arrays a;
cout << "\nEnter the size: ";
cin >> a.n;
a.insertion();
cout << "\nThe array is: ";
a.display();
a.removedupli();
cout << "\nthe array after removing the duplicates: ";
a.display();
return 0;
}