How to find first non repeated character of a given String?

Asked 27-Feb-2018
Viewed 305 times

1 Answer


0

"Code for finding the first non repeating character  of a given String"

class FindNonReaptingCharacter
{
    /* This method returns index of the first non-repeating
       character in a string. If all characters are repeating
       then it returns -1 */

    static int firstNonRepeatingCharacter(String str)
    {
        int index = -1, i;
        for (i = 0; i < str.length(); i++)
        {
         for(j=0;j<str.length(); i++)
         {
             if (str.charAt(i)==str.charAt[j])
                    {
                index = i;
                  break;
                  }
             }
}
      return index;
    }
    // Main Method
    public static void main (String[] args)
    {
        String str = "mindstick.com";
        int index = firstNonRepeatingCharacter(str);
        System.out.println(index == -1 ? "Either all characters are repeating " : "First non-repeating character is : : " + str.charAt(index));
    }
}