Find the first reoccuring char from a String Can anyone give me advice on how I can improve my answer?
I have tested it out using "ABCA", "BCABA" and "ABC".
Two things I would like help on.
1.How can I move this code into a method
where it returns the reoccurring char? I know how to return the char once it has been found but what about the case where there is not char what do I return back in that case? Since I cannot return char.
2. Is using the break statement bad practice? I noticed that the code would with "BCABA" both "B" and "A" are printed.

Thanks.

  public static void main(String[] args) {
    String testString = "BCABA";

    Map&lt;Character, Integer&gt; recurringCharsMap = new HashMap&lt;&gt;();
    for (char character : testString.toCharArray()) {
        if (recurringCharsMap.containsKey(character)) {
            System.out.println("Recurring char is: " + character);
            break;
        } else {
            recurringCharsMap.put(character, 1);
        }
    }
}
The Character type already has everything you need. It is either a character or the special value null.

For this task you don't need a map, a simple set is enough.

When you move this code into a separate method, using break becomes bad style because you should directly return. In the currect code it's ok.

public static Character findFirstRecurring(String str, char ch) {
    // Left as an exercise
    …
    return null;
}