Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
find first non repeating
// 3. Find first non-repeating character in String // ex: geeksforgeeks: f // geeksforgeeksFirst:o #include <iostream> #include <string> #include <unordered_map> using namespace std; char findFirstNoneRepeating(const string& s) { unordered_map<char, int> m; for (unsigned i = 0; i < s.length(); ++i) { char c = tolower(s[i]); if (m.find(c) == m.end()) m[c] = 1; else ++m[c]; } auto best = m.begin(); for (auto it = m.begin(); it != m.end(); ++it) if (it->second <= best->second) best = it; return (best->first); } int main() { cout << findFirstNoneRepeating("gggccaaabbb") << endl; cout << findFirstNoneRepeating("geeksforgeeks") << endl; cout << findFirstNoneRepeating("geeksforgeeksFirst") << endl; }
run
|
edit
|
history
|
help
0
my template
fundamental type sizes
Vector
Inherit
Assigment operator (easy)
123
typecasting
sheetal
Dar
constructing object on first use as return value of (pointer to) object-returning function