Find First Non Repeating Character in a String in Java
Now Here we are going print First non repeated character in String in java through a simple example or java program so that you can understand well.
So let's start a simple java program for find the first non repeated character of a String.
For example, we take a string in java like String str = "akalsys";
So in above string there is k is a first non repeated character in String.
Java Example for First Non-Repeated Character in String.
class FirstNonRepeatedCharacterInString
{
public static void main(String args[])
{
String str = "akalsys";
for(char ch : str.toCharArray( ))
{
if(str.indexOf(ch) == str.lastIndexOf(ch))
{
System.out.println(ch);
break;
}
}
}
}
Output : k
Above example will you give the k as output.
Here we learned, how to find the first non repeated character in java string with a simple example.
very very useful article specially for java interviewer because this question is mostly asked in java interviews.
ReplyDelete