-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency.cpp
More file actions
34 lines (27 loc) · 810 Bytes
/
frequency.cpp
File metadata and controls
34 lines (27 loc) · 810 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
#include <bits/stdc++.h>
using namespace std;
void highestFrequencyElement(int arr[], int n) {
unordered_map<int, int> frequencyMap;
for (int i = 0; i < n; i++) {
frequencyMap[arr[i]]++;
}
int maxFrequency = 0;
int elementWithMaxFrequency = arr[0];
for (auto it : frequencyMap) {
if (it.second > maxFrequency) {
maxFrequency = it.second;
elementWithMaxFrequency = it.first;
}
}
cout << "Element with highest frequency: " << elementWithMaxFrequency << " (Frequency: " << maxFrequency << ")\n";
}
int main() {
int n;
cin >> n; // size of the array
int arr[n];
for (int i = 0; i < n; i++) { // input array elements
cin >> arr[i];
}
highestFrequencyElement(arr, n);
return 0;
}