-
Notifications
You must be signed in to change notification settings - Fork 256
/
23 Finding Duplicate in Soorted Array.cpp
71 lines (53 loc) · 1.47 KB
/
23 Finding Duplicate in Soorted Array.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
#include<iostream>
using namespace std;
struct Array {
int* A;
int size;
int length;
};
void Find_Duplicate(struct Array* arr) {
int last_duplicate = 0;// initially set duplicate as zero
for (int i = 0; i < arr->length; i++) { // iterate through the list of elements
if (arr->A[i] == arr->A[i + 1] && arr->A[i] != last_duplicate) // if consequtive elements are equal then consider as duplicate
{
cout << arr->A[i]<<" ";
last_duplicate = arr->A[i]; // everytine after printing duplicate update it will next duplicate element
}
}
}
int main() {
struct Array arr;
int no;
cout << "Enter the size of the array " << endl;
cin >> arr.size;
arr.A = new int[arr.size];
arr.length = 0;
cout << "Enter the size of the array" << endl;
cin >> no;
cout << "Enter the elements of the array " << endl;
for (int i = 0; i < no; i++)
cin >> arr.A[i];
arr.length = no;
cout << "The duplicate elements are ! " << endl;
Find_Duplicate(&arr);
return 0;
}
// -------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main ()
{
int A[10]={3,6,8,8,10,12,15,15,15,20};
int lastduplicate=0;
int i,no=10;
cout<<"Duplicate Elements are "<<endl;
for (i=0;i<no;i++)
{
if (A[i]==A[i+1] && A[i]!=lastduplicate)
{
cout<<" "<<A[i];
lastduplicate=A[i];
}
}
return 0;
}