Skip to content

Commit 4fea113

Browse files
committed
Added Frequency of each elements
1 parent 5caadf4 commit 4fea113

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

FrequencyEachElement.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
5+
void frequency(vector<int>& arr) {
6+
int size = arr.size();
7+
8+
vector<bool> visited(size, false);
9+
10+
for(int i = 0; i < size; i++) {
11+
if(visited[i] == true) continue;
12+
13+
int count = 1;
14+
for(int j = i+1; j < size; j++) {
15+
if(arr[i] == arr[j]) {
16+
visited[j] = true;
17+
count++;
18+
}
19+
}
20+
cout << arr[i] << ": " << count << endl;
21+
}
22+
}
23+
24+
int main() {
25+
vector<int> a1 = {1,1,1,3,4,5,6,6,5,4,6,7,4,3,3};
26+
cout << "CASE #1: ";
27+
for(int e: a1) cout << e << " ";
28+
cout << endl;
29+
frequency(a1);
30+
31+
vector<int> a2 = {45,56,3,3,4,5,7,8,8,34,3,5,235,2,35,25,236,2,35,26,23,34};
32+
cout << "\nCASE #2: ";
33+
for(int e: a2) cout << e << " ";
34+
cout << endl;
35+
frequency(a2);
36+
37+
vector<int> a3 = {435,5,23,2,35,23,5,25,6,7,3,7,456,345,435,2};
38+
cout << "\nCASE #3: ";
39+
for(int e: a3) cout << e << " ";
40+
cout << endl;
41+
frequency(a3);
42+
43+
return 0;
44+
}

0 commit comments

Comments
 (0)