Created
November 11, 2021 10:34
-
-
Save paulosuzart/ed1a32723bebd9ea369df17a719e96ec to your computer and use it in GitHub Desktop.
first repeated char
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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