-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy path24 Counting Duplicate Elements In Sorted.cpp
71 lines (61 loc) · 1.65 KB
/
24 Counting Duplicate Elements In Sorted.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 Count_Find_duplicate(struct Array* arr) {
int i;
for (i = 0; i < arr->length - 1; i++) {
if (arr->A[i] == arr->A[i + 1]) // if the consequtive term of the array is equal
{
int j = i + 1; // set j one next to i and increment as long as both are equal
while (arr->A[j] == arr->A[i]) j++;
cout << arr->A[i] << " is duplicate element and appering for " << j - i << " Times";// j-i will give total number of duplicate element
i = j - 1; // set i before one before j
}
}
}
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;
Count_Find_duplicate(&arr);
// Display(arr);
return 0;
}
// Time complexity is O(n)
// Here while loop is negligible as compared to for loop
// --------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std ;
int main ()
{
int i,j;
int A[13]={3,6,8,8,8,10,12,12,12,15,20,20,20};
for(i=0;i<13;i++)
{
if (A[i]==A[i+1])
{
j=i+1;
while (A[j]==A[i])
j++;
cout<<A[i]<<" Appering "<<j-i<<" Times "<<endl;
i=j-1;
}
}
cout<<endl;
return 0;
}
// Time complexity is O(n)
// Here while loop is negligible as compared to for loop