Created
March 22, 2018 12:34
-
-
Save eduardoaugustojulio/0e009bc166beee20b97cba7bacbfdf51 to your computer and use it in GitHub Desktop.
find most ocurrence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <map> | |
#include <chrono> | |
int findMostOcurrence(std::vector<std::string> v, std::string &ocrn) | |
{ | |
auto start = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()).time_since_epoch().count(); | |
int count = 0; | |
std::map<std::string, int> m; | |
for ( auto c : v ) | |
{ | |
auto it= m.find(c); | |
if( it != m.end() ) | |
m[c]++; | |
else | |
m[c] = 1; | |
} | |
for ( auto mi : m ) | |
if ( mi.second > count ) | |
{ | |
ocrn = mi.first; | |
count = mi.second; | |
} | |
auto end = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()).time_since_epoch().count(); | |
std::cout << "Time elapsed " << (end - start) << " ms in "<< __FUNCTION__ << std::endl; | |
return count; | |
} | |
int main(const int argc, const char **argv) | |
{ | |
std::string var; | |
std::vector<std::string> possible_values = {"fernanda", "joao", "fernanda", "francisco"}; | |
std::cout << findMostOcurrence(possible_values, var) << " " << var << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment