-
Notifications
You must be signed in to change notification settings - Fork 192
/
most_occuring_character.cpp
42 lines (35 loc) · 981 Bytes
/
most_occuring_character.cpp
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
#include <iostream>
#include <unordered_map>
#include <vector>
void findMostOccChar(const std::string& str) {
std::unordered_map<char, int> charCount;
// Count the occurrence of each character in the string
for (char c : str) {
charCount[c]++;
}
int maxCount = 0;
// Finding the maximum occurrence of a character
for (const auto& entry : charCount) {
if (entry.second > maxCount) {
maxCount = entry.second;
}
}
// Collect the most occurring character(s)
std::vector<char> mostOccChars;
for (const auto& entry : charCount) {
if (entry.second == maxCount) {
mostOccChars.push_back(entry.first);
}
}
// Printing the most occurring character(s) and its count
for (char c : mostOccChars) {
std::cout << "Character: " << c << ", Count: " << maxCount << std::endl;
}
}
int main() {
std::string inputString = "helloworldmylovelypython";
findMostOccChar(inputString);
return 0;
}
// Answer: /tmp/Y4XZ1a4DwQ.o
// Character: l, Count: 5