4 Ways to Find First Non Repeated Character in String in Java

In this post, we will write a program that finds a first non repeated character in string in Java.
We will discuss 4 different ways to find a first non repeated character in string in Java.
  1. Loop and break solution
  2. 256 ASCII codes solution
  3. LinkedHashMap based solution
  4. Java 8, functional-style solution

4 Ways to Find First Non-Repeated Character in String in Java

Here are four different methods that find the non repeated character in string in Java:
  1. firstNonRepeatedCharacterV1()
  2. firstNonRepeatedCharacterV2()
  3. firstNonRepeatedCharacterV3()
  4. firstNonRepeatedCharacterV4()
Here is the complete Java program:
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class FirstNonRepeatedCharacter {

    private static final int EXTENDED_ASCII_CODES = 256;

    private static final String TEXT = "Java Guides Popular blog Website";

    public static void main(String[] args) {

        System.out.println("Input text: \n" + TEXT + "\n");

        System.out.println("Loop and break solution:");
        char firstcharV1 = firstNonRepeatedCharacterV1(TEXT);

        System.out.println("Found character: " + firstcharV1);

        System.out.println();
        System.out.println(" 256 ASCII codes solution:");

        char firstcharV2 = firstNonRepeatedCharacterV2(TEXT);
        System.out.println("Found character: " + firstcharV2);

        System.out.println();
        System.out.println("LinkedHashMap based solution:");
        char firstcharV3 = firstNonRepeatedCharacterV3(TEXT);
        System.out.println("Found character: " + firstcharV3);

        System.out.println();
        System.out.println("Java 8, functional-style solution:");
        char firstcharV4 = firstNonRepeatedCharacterV4(TEXT);
        System.out.println("Found character: " + firstcharV4);
    }

    public static char firstNonRepeatedCharacterV1(String str) {

        if (str == null) {
            // or throw IllegalArgumentException
            return Character.MIN_VALUE;
        }

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

            char ch = str.charAt(i);

            int count = 0;
            for (int j = 0; j < str.length(); j++) {
                if (ch == str.charAt(j) && i != j) {
                    count++;
                    break;
                }
            }

            if (count == 0) {
                return ch;
            }
        }

        return Character.MIN_VALUE;
    }

    public static char firstNonRepeatedCharacterV2(String str) {

        if (str == null) {
            // or throw IllegalArgumentException
            return Character.MIN_VALUE;
        }

        int[] flags = new int[EXTENDED_ASCII_CODES];

        for (int i = 0; i < flags.length; i++) {
            flags[i] = -1;
        }

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

            if (flags[str.charAt(i)] == -1) {
                flags[str.charAt(i)] = i;
            } else {
                flags[str.charAt(i)] = -2;
            }
        }

        int position = Integer.MAX_VALUE;
        for (int i = 0; i < EXTENDED_ASCII_CODES; i++) {
            if (flags[i] >= 0) {
                position = Math.min(position, flags[i]);
            }
        }

        return position == Integer.MAX_VALUE ? Character.MIN_VALUE : str.charAt(position);
    }

    public static char firstNonRepeatedCharacterV3(String str) {

        if (str == null) {
            // or throw IllegalArgumentException
            return Character.MIN_VALUE;
        }

        Map < Character, Integer > chars = new LinkedHashMap < > ();

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

            Integer count = chars.get(ch);
            if (count == null) {
                chars.put(ch, 1);
            } else {
                chars.put(ch, ++count);
            }
        }

        for (Map.Entry < Character, Integer > entry: chars.entrySet()) {
            if (entry.getValue() == 1) {
                return entry.getKey();
            }
        }

        return Character.MIN_VALUE;
    }

    public static char firstNonRepeatedCharacterV4(String str) {

        if (str == null) {
            // or throw IllegalArgumentException
            return Character.MIN_VALUE;
        }

        Map < Integer, Long > chs = str.chars().boxed()
            .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));

        return (char)(int) chs.entrySet().stream().filter(e -> e.getValue() == 1 L).findFirst().map(Map.Entry::getKey)
            .orElse(Integer.valueOf(Character.MIN_VALUE));
    }
}

Output

Input text: 
Java Guides Popular blog Website

Loop and break solution:
Found character: J

 256 ASCII codes solution:
Found character: J

LinkedHashMap based solution:
Found character: J

Java 8, functional-style solution:
Found character: J


Comments