How to count the unique characters in a string with C++

1 Answer

0 votes
#include <iostream>
#include <unordered_set>

int count_unique_char(std::string str) {
    std::unordered_set<char> st;
 
    for (int i = 0; i < str.size(); i++) {
        st.insert(str[i]);
    }
 
    return st.size();
}
 
int main() {
    std::string str = "javascript typescript node.js c c++";
 
    std::cout << count_unique_char(str);
}
 
 
 
 
 
 
/*
run:
 
17
 
*/

 



answered May 4, 2022 by avibootz

Related questions

1 answer 149 views
1 answer 124 views
1 answer 125 views
2 answers 164 views
1 answer 139 views
1 answer 131 views
1 answer 118 views
...