more katas
This commit is contained in:
42
Counting Duplicates/main.cpp
Normal file
42
Counting Duplicates/main.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
size_t duplicateCount(const std::string& in); // helper for tests
|
||||
|
||||
std::string toLower(const char* input){
|
||||
std::string temp = input;
|
||||
std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
|
||||
return temp;
|
||||
}
|
||||
|
||||
size_t duplicateCount(const char* in)
|
||||
{
|
||||
// Convert to lower chars
|
||||
std::string input = toLower(in);
|
||||
|
||||
// Start counting for every char
|
||||
std::map<char, int> my_map;
|
||||
for(auto character: input){
|
||||
auto it = my_map.find(character);
|
||||
if(it != my_map.end()){
|
||||
it->second += 1;
|
||||
} else {
|
||||
my_map[character] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Count how often we have a value higher than 1
|
||||
int count = 0;
|
||||
for(auto it: my_map){
|
||||
if(it.second > 1)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::cout << duplicateCount("TESTtest");
|
||||
}
|
||||
Reference in New Issue
Block a user