Find duplicate characters in a string c++ code example
Example 1: count occurrences of character in string c++
std::string s = "a_b_c";
size_t n = std::count(s.begin(), s.end(), '_'); // n=2
Example 2: finding no of unique characters in a string c++
int countDistinct(string s)
{
unordered_map<char, int> m;
for (int i = 0; i < s.length(); i++) {
m[s[i]]++;
}
return m.size();
}