Skip to content

Instantly share code, notes, and snippets.

@paulosuzart
Created November 11, 2021 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulosuzart/ed1a32723bebd9ea369df17a719e96ec to your computer and use it in GitHub Desktop.
Save paulosuzart/ed1a32723bebd9ea369df17a719e96ec to your computer and use it in GitHub Desktop.
first repeated char
public class Application {
static String solve(String word) {
Objects.requireNonNull(word);
Map<String, Integer> chars = new LinkedHashMap<String, Integer>();
for (int i = 0; i < word.length(); i++) {
var w = word.charAt(i);
chars.compute(String.valueOf(w), (k, v) -> {
if (v != null) {
return v + 1;
}
return 1;
});
}
return chars.entrySet().stream().filter(entry -> entry.getValue().equals(1)).findFirst()
.map(e -> e.getKey()).orElse("");
}
public static void main(String[] args) {
System.out.println(solve("swiss"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment