First Letter to Appear Twice LeetCode solution in Java

1
class Solution {
2
public char repeatedCharacter(String s) {
3
HashSet<Character> set = new HashSet<>(); // Create a set of characters
4
for (int i = 0; i < s.length(); i++) {
5
if (set.contains(s.charAt(i)))
6
return s.charAt(
7
i); // If the set already contains the current character, then it is the required ans
8
set.add(s.charAt(i));
9
}
10
return 'a'; // As it is given in the question that there is at least one letter that appears
11
// twice, therefore it is certain that the ans will be found before we reach this
12
// statement. So, just adding any random return statement so that there is no error
13
// in the code.
14
}
15
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0