-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap_Analyzer.cpp
More file actions
27 lines (22 loc) · 822 Bytes
/
HashMap_Analyzer.cpp
File metadata and controls
27 lines (22 loc) · 822 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
#include "HashMap_Analyzer.h"
#include <unordered_map>
using namespace std;
int HashMapAnalyzer::getMostCommonLength(const vector<int>& lengths) {
//creating a hash table to count frequency; increases count for each element in the length's vector
unordered_map<int, int> frequency;
for (int lenth : lengths) {
frequency[lenth]++;
}
//variables to keep track of the length that has the highest frequency
int maxLenth = 0;
int maxFrequency = 0;
//iterate through frequency table to find the most common lenght and update those values
for (const auto& [lenth, count] : frequency) {
if (count > maxFrequency) {
maxFrequency = count;
maxLenth = lenth;
}
}
//returning the length that appeared the most often
return maxLenth;
}