Here we pass the string in a for loop and through charAt method we converted in character. from indexOf() method returns the position of the first occurrence of a given character in a string whereas method lastIndexOf() method returns the position of the last occurrence of a given character in a string. if the indexOf() and lastIndexOf() methods return the same position of given character are the same, then that character is the first non repeat character in a string.
public class FirstNonRepeatChar {
public static String repeat(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (str.indexOf(c) == i && str.indexOf(c, i + 1) == -1) {
return String.valueOf(c);
}
}
return "non repeat";
}
public static void main(String[] args) {
String str = "wwellcomewe";
System.out.println(FirstNonRepeatChar.repeat(str));
}
}