Left most non repetitive character in string in java

0

 


In this blog we will learn about how to print left most non repetitive character of string. In this we are given a string and we have to find the left most non repetitive character in java.



public class LeftMostNonRepeating {
public static void main(String args[])
{
int arr[]=new int[26];
for(int i=0;i<26;i++)
{
arr[i]=0;
}
String str="apple";
int min=str.length();
for(int i=0;i<str.length();i++)
{
arr[str.charAt(i)-'a']++;
}
for(int i=0;i<26;i++)
{
if(arr[i]>0)
System.out.println((char)(97+i)+" "+arr[i]);
}
for(int i=0;i<26;i++)
{
if(arr[i]==1)
{
int val=str.indexOf((char)(97+i));
if(min>val)
{
min=val;
}

}
}
System.out.println(min);
}
}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !