Java Program to Find the First Repeated Character in a String
In this section, we will show you three different ways to find first repeated character in a given String in Java. 1. Using For loop and Map 2. Using Java 9 chars() method 3. Using Java 8 Streams Example 1. Using For loop and Map import java.util.LinkedHashMap ; import java.util.Map ; public class Main { public static void main ( String [] args) { String str = "javaworld" ; Map < Character , Integer > map = new LinkedHashMap<>(); for ( Character character : str .toCharArray()) { if ( map .containsKey( character )) { map .put( character , map .get( character ) + 1 ); } else { map .put( character , 1 ); } if ( map .get( character ) > 1 ) { System . out .println( character ); break ; } } } } Here the logic is simple, toCharArray() convert String to char Array. Iterate over Character Array and check