Java program to Find First non repeating character in a String

Java Program to find the First non repeating character in a string

In this article, we will learn how to code a java program to find the first non repeating character in a string 
Java program to find non repeating characters in a string

Method discussed

  • Method 1 – Using indexOf() and lastIndexOf() methods
  • Method 2 – This method builds a frequency array
  • Method 3 – This method uses Linked Hashmap
  • Method 4 – This method uses Set and ArrayList
  • Method 5 – This method uses Java 8
Find First non repeating character in a String in Java

Method 1

Using indexOf() and lastIndexOF() methods.

As soon as we find the first item that only appears once print stop further.

Run
class Main
{
    public static void main(String args[])
    {
        String inputStr ="prepinsta";
        boolean flag = true;

        for(char i :inputStr.toCharArray())
        {
            // if current character is the last occurrence in the string
            if (inputStr.indexOf(i) == inputStr.lastIndexOf(i))
            {
                System.out.println("First non-repeating character is: "+ i);
                flag = false;
                break;
            }
        }

        if(flag)
            System.out.println("There is no non repeating character in input string");
    }
}

Output

First non-repeating character is: r

Method 2

  • This method builds a frequency array
  • With a frequency count of each ASCII character between 0 – 255
  • We then find iterate the input string from the start
  • Print the first character with frequency count == 1

Run

class Main {
    static final int NO_OF_CHARS = 256;
    static char count[] = new char[NO_OF_CHARS];

    // this method builds count array which updates frequency for each
    // ascii character (0-255)
    // example for 'p' ascii value is 112, in the array
    // count[112] value would be 2 (p appears twice)
    static void buildCharCountArray(String str)
    {
        for (int i = 0; i < str.length(); i++) {
            count[str.charAt(i)]++;
        }
    }

    // this method finds first non repeating character
    // returns the index of the first non repeating character
    static int findFirstNonRepeating(String string)
    {
        // build char count array
        buildCharCountArray(string);

        int pos = -1, i;

        for (i = 0; i < string.length(); i++) {
            if (count[string.charAt(i)] == 1) {
                pos = i;
                break;
            }
        }

        return pos;
    }

    // Driver method
    public static void main(String[] args)
    {
        String string = "prepinsta";
        int pos = findFirstNonRepeating(string);

        System.out.println(
                pos == -1
                        ? "All characters are repeating "
                        : "First non-repeating character is "
                        + string.charAt(pos));
    }
}

Output

First non-repeating character is: r

Method 3

This method uses Linked Hashmap

Run
import java.util.*;
import java.util.Map.Entry;

class Main {

    public static void main(String[] args)
    {

        String string = "prepinsta";
        char ch = findFirstNonRepeating(string);
            System.out.println("First non-repeating character is : " + ch);
    }

    public static Character findFirstNonRepeating(String str)
    {
        HashMap  charhashtable =
                new LinkedHashMap();
        int len;
        Character ch;
        len = str.length();  // Scan string and build hash table

        for (int i = 0; i < len; i++)
        {
            ch = str.charAt(i);

            if(charhashtable.containsKey(ch))
                // increment count corresponding to ch
                charhashtable.put(ch, charhashtable.get(ch) + 1);
            else
                charhashtable.put(ch , 1);

        }
        for(Entry entry: charhashtable.entrySet())
        {
            if(entry.getValue() == 1)
                return entry.getKey();
        }
        return null;
    }
}

Output

First non-repeating character is: r

Method 4

This method uses Set and ArrayList

Run
import java.util.*;

class Main {


    public static Character findFirstNonRepeating(String str)
    {
        // set stores characters that are repeating
        Set charRepeatingSet = new HashSet<>();

        // ArrayList stores characters that are non repeating
        List charNonRepeatingList = new ArrayList<>();

        for(int i=0; i < str.length(); i++)
        {

            char ch = str.charAt(i);

            // if set already contains character ch
            if(charRepeatingSet.contains(ch))
                continue;

            // if Array List contains character ch
            // remove existing ch from ArrayList and
            // add ch to set
            if(charNonRepeatingList.contains(ch)) {
                charNonRepeatingList.remove((Character) ch);
                charRepeatingSet.add(ch);
            }

            // else add to ArrayList as character must be first occurrence
            else {
                charNonRepeatingList.add(ch);
            }
        }
        return charNonRepeatingList.get(0);
    }

    public static void main(String[] args)
    {

        String string = "prepinsta";
        char ch = findFirstNonRepeating(string);
        System.out.println("The first non repeated character is :  " + ch);
    }
}

Output

First non-repeating character is: r

Method 5

This method uses Java 8

Run
import java.util.*;

class Main {

    public static Character findFirstNonRepeating(String string) {
        Map map = new LinkedHashMap();

        for (Character ch : string.toCharArray()) {
            map.put(ch, map.containsKey(ch) ? map.get(ch) + 1 : 1);
        }
        return map.entrySet().stream().filter(x -> x.getValue() == 1).findFirst().get().getKey();
    }


    public static void main(String[] args)
    {
        String string = "prepinsta";
        char character = findFirstNonRepeating(string);
        System.out.println("First non-repeating character is : " + character);
    }
}

Output

First non-repeating character is: r

Algorithm :

  • Take String as a input from the user.
  • Create array of 256 element to store frequency of each character.
  • Iterate a loop from 0 to length of the string to calculate frequency of each character.
  • Again iterate a loop from 0 to 256 to find the character whose frequency is 1 and also print that character.
  • we will get the desired output.

Solution in Java for finding the non repeating characters in a string

Run
class Main
{
    public static void main(String args[])
    {
        String inputStr ="prepinsta";
        boolean flag = true;

        for(char i :inputStr.toCharArray())
        {
            // if current character is the last occurrence in the string
            if (inputStr.indexOf(i) == inputStr.lastIndexOf(i))
            {
                System.out.println("First non-repeating character is: "+ i);
                flag = false;
                break;
            }
        }

        if(flag)
            System.out.println("There is no non repeating character in input string");
    }
}

Output :

Enter the string
prepinsta
The non repeating characters are :
a e i n r s t

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

  • Length of the string without using strlen() function : C | C++ | Java | Python
  • Toggle each character in a string : C | C++ | Java | Python
  • Count the number of vowels : C | C++ | Java | Python
  • Remove the vowels from a String : C | C++ | Java | Python
  • Check if the given string is Palindrome or not : C | C++ | Java | Python
  • Print the given string in reverse order : C | C++ | Java | Python
  • Remove all characters from string except alphabets : C | C++ | Java | Python
  • Remove spaces from a string : C | C++ | Java | Python
  • Remove brackets from an algebraic expression : C | C++ | Java | Python
  • Count the sum of numbers in a string : C | C++ | Java | Python
  • Capitalize the first and last character of each word of a string : C | C++ | Java | Python
  • Calculate frequency of characters in a string : C | C++ | Java | Python
  • Find non-repeating characters in a string : C | C++ | JAVA | Python
  • Check if two strings are Anagram or not : C | C++ | Java | Python
  • Replace a sub-string in a string: C | C++ | Java | Python
  • Count common sub-sequence in two strings : C | C++ | Java | Python
  • Check if two strings match where one string contains wildcard characters : C | C++ | Java | Python

4 comments on “Java program to Find First non repeating character in a String”


  • Deepikala V

    import java.util.*;
    class Nonrepeat{
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();
    StringBuilder sb=new StringBuilder(s);
    //char[] c=s.toCharArray();
    int flag;
    for(int i=0;i<sb.length();i++)
    {
    flag=0;
    for(int j=i+1;j<sb.length();j++)
    {
    if(sb.charAt(i)==sb.charAt(j)){
    flag=1;
    sb.deleteCharAt(j);
    }
    }
    if(flag==0)
    System.out.print(sb.charAt(i));
    else
    continue;
    }
    }}


  • Ankitha

    public class NonReapChars {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    String str =sc.nextLine();
    char[] chars=str.toCharArray();
    int flag=0;
    for(int i =0; i<chars.length; i++) {
    flag=0;
    for(int j=i+1; j<chars.length; j++) {
    if(chars[i]==chars[j]) {
    flag=1;
    }
    }
    if(flag==0) {
    System.out.println(chars[i]);
    break;
    }
    }
    }

    }


  • Chelliboina

    import java.util.*;
    public class Main
    {
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();
    char c[]=s.toCharArray();
    int count=0;
    for(int i=0;i<c.length;i++)
    {
    count=0;
    for(int j=i+1;j0)
    {
    c[i]=’0′;
    }
    }
    for(int i=0;i<c.length;i++)
    {
    if(c[i]!='0'&& c[i]!=' ')
    {
    System.out.println(c[i]);
    break;
    }
    }
    }
    }