forked from Nikhil-2002/Programming_Hactoberfest25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_frequency_of_array_items.cpp
More file actions
63 lines (47 loc) · 1.02 KB
/
Copy pathcount_frequency_of_array_items.cpp
File metadata and controls
63 lines (47 loc) · 1.02 KB
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
// CPP program to count frequencies of array items✅
#include <bits/stdc++.h>
using namespace std;
void countFreq(int arr[], int n)
{
// Mark all array elements as not visited
vector<bool> visited(n, false);
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Skip this element if already processed
if (visited[i] == true)
continue;
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
visited[j] = true;
count++;
}
}
cout << arr[i] << " " << count << endl;
}
}
//Main code
int main()
{
int arr[] = { 10, 20, 20, 10, 10, 20, 5, 20 };
int n = sizeof(arr) / sizeof(arr[0]);
countFreq(arr, n);
return 0;
}
// Complexity Analysis:
// Time Complexity : O(n2)
// Auxiliary Space : O(n)
// Output
// 10 3
// 20 4
// 5 1
// Examples:
// Input : arr[] = {20, 20, 10, 10, 20, 5, 20}
// Output : 10 2
// 20 4
// 5 1
// Input : arr[] = {10,20}
// Output : 10 1
// 20 1