-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighest-occuring-element.cpp
More file actions
73 lines (62 loc) · 2.11 KB
/
highest-occuring-element.cpp
File metadata and controls
73 lines (62 loc) · 2.11 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
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int highestOccurringElement (vector<int>& nums)
{
// store max occurring element with frequency in variable
int maxEle = 0;
int maxFreq = 0;
// create unordered map to store key and value pairs
unordered_map<int, int> umap;
// add the values of nums in umap
for (int i=0; i<nums.size(); i++) {
umap[nums[i]]++;
}
// loop through again and check the max frequency
for (auto it : umap) {
int ele = it.first;
int freq = it.second;
if (maxFreq < freq) {
maxFreq = freq;
maxEle = ele;
} else if (maxFreq == freq) {
if (ele < maxEle) {
maxEle = ele;
}
}
}
return maxEle;
}
/*
Bruteforce:
int calc (int arr[], int size) {
int visitor[10000 + 1] = {0};
int maxCnt = -1;
int ele = -1;
for (int i=0; i<size; i++) {
int count = 0;
if (visitor[arr[i]] == 0) {
for (int j=i; j<size; j++) {
// cout << "arr[i]: " << arr[i] << " arr[j]: " << arr[j] << endl;
if (arr[i] == arr[j]) {
count++;
}
}
if (maxCnt < count) {
// cout << "maxCnt: " << maxCnt << endl;
maxCnt = count;
ele = arr[i];
}
visitor[arr[i]] = 1;
}
}
return ele;
}
*/
};
int main() {
Solution sol;
vector<int> nums = {1, 2, 3, 4, 5, 5, 3};
cout << sol.highestOccurringElement(nums);
}