-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_Count_Duplicates.cpp
More file actions
43 lines (39 loc) · 900 Bytes
/
20_Count_Duplicates.cpp
File metadata and controls
43 lines (39 loc) · 900 Bytes
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
#include <iostream>
using namespace std;
int main()
{
int arr[10] = {3, 6, 8, 8, 10, 12, 15, 15, 15, 20};
// int dup = 0;
// for (int i = 0; i < 10; i++)
// {
// if (arr[i] == arr[i + 1])
// {
// dup = i + 1;
// while (arr[dup] == arr[i])
// {
// dup++;
// }
// cout << arr[i] << " is appearing " << dup - i << " times" << endl;
// i = dup - 1;
// }
// }
// Method 2
int H[20] = {0};
int lastDup = 0;
for (int i = 0; i < 10; i++)
{
while (arr[i] == arr[i + 1] || arr[i] == lastDup)
{
lastDup = arr[i];
H[arr[i]]++;
i++;
}
}
for (int i = 0; i < 20; i++)
{
if (H[i] != 0)
{
cout << i << " Appeared " << H[i] << " times" << endl;
}
}
}