Java Program to Find the First Non-repeated Character in a String

This is a commonly asked Java program during technical interviews as it tests one's understanding of data structures and string manipulation. In this blog post, we'll break down how to find the first non-repeated character in a string using Java. We'll walk you through it step by step, ensuring it's easy for beginners to understand.

The Strategy 

Iterate through the string to build a frequency count for each character. 

Iterate again through the string to find the first character with a frequency count of 1. 

We can efficiently achieve this with the help of data structures like HashMap. 

The Java Program Let's dive into the code:

import java.util.LinkedHashMap;
import java.util.Map;

public class NonRepeatedCharacterFinder {

    public static void main(String[] args) {
        String text = "Java Guides";
        char result = firstNonRepeatedCharacter(text);
        if (result != '\0') {
            System.out.println("The first non-repeated character is: " + result);
        } else {
            System.out.println("All characters are repeated.");
        }
    }

    public static char firstNonRepeatedCharacter(String input) {
        // Step 1: Build a frequency count for each character
        Map<Character, Integer> charCount = new LinkedHashMap<>();
        for (char ch : input.toCharArray()) {
            charCount.put(ch, charCount.getOrDefault(ch, 0) + 1);
        }

        // Step 2: Find the first character with a count of 1
        for (char ch : input.toCharArray()) {
            if (charCount.get(ch) == 1) {
                return ch;
            }
        }

        return '\0'; // return null character if all characters are repeated
    }
}

Explaining the Program Step by Step

Imports:

import java.util.LinkedHashMap;
import java.util.Map;

We import the necessary classes. LinkedHashMap is a HashMap that maintains the insertion order, which is crucial to finding the first non-repeated character.

Building Character Frequency Count:

Map<Character, Integer> charCount = new LinkedHashMap<>();
for (char ch : input.toCharArray()) {
    charCount.put(ch, charCount.getOrDefault(ch, 0) + 1);
}

We iterate through each character in the string, and for each character, we increase its frequency count. If the character doesn't exist in our charCount map, getOrDefault will return 0.

Finding the First Non-repeated Character:

for (char ch : input.toCharArray()) {
    if (charCount.get(ch) == 1) {
        return ch;
    }
}

By iterating through the string again and checking the frequency count, we find the first character with a count of 1. This is our first non-repeated character. 

Handling No Unique Characters: 

If all characters in the string are repeated, we return the null character ('\0'). This provides a way to communicate that no unique character was found. 

Output: 

Given the sample text "Java Guides", when you run the program, you will see:
The first non-repeated character is: J

If you pass sample text "mahimahi", when you run the program, you will see:

All characters are repeated.
There are no non-repeating characters in the above input – all characters repeat once. So the output here is All characters are repeated.

Conclusion

Congratulations! You now understand how to write a Java program to identify the first non-repeated character in a string. This exercise teaches the essentials of string manipulation, character iteration, and efficient usage of data structures like HashMap. Keep practicing, and soon these concepts will become second nature!

Related Java String Programs with Output

Comments