First Letter to Appear Twice LeetCode solution in C++

1
class Solution {
2
public:
3
char repeatedCharacter(string s) {
4
unordered_map<char, int> mp; // for storing occurrences of char
5

6
char ans;
7
for (auto it : s) {
8
if (mp.find(it) != mp.end()) // any char which comes twice first will be the ans;
9
{
10
ans = it;
11
break;
12
}
13
mp[it]++; // increase the count of char
14
}
15
return ans;
16
}
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0