Find First Non-Repeated Character in Java

In this post, we’ll write a Java program to find the first non-repeated character in a string. This is a popular question in coding interviews to test your understanding of string manipulation and hashing.

๐Ÿ“Œ Problem Statement

Given a string, find the first character that does not repeat anywhere else in the string. Return that character.

๐Ÿ“š Example

  • Input: "programming" → Output: 'p'
  • Input: "aabbcc" → Output: No unique character

✅ Java Program Using LinkedHashMap


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

public class FirstUniqueCharacter {
    public static void main(String[] args) {
        String input = "programming";
        Character result = findFirstNonRepeated(input);

        if(result != null)
            System.out.println("First non-repeated character is: " + result);
        else
            System.out.println("No unique character found.");
    }

    static Character findFirstNonRepeated(String str) {
        Map charCount = new LinkedHashMap<>();

        for (char c : str.toCharArray()) {
            charCount.put(c, charCount.getOrDefault(c, 0) + 1);
        }

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

        return null;
    }
}

✅ Output

First non-repeated character is: p

๐Ÿง  Interview Tip

This question can be optimized by using LinkedHashMap for maintaining order. Make sure you explain your choice of data structures clearly.

๐Ÿ“Œ Conclusion

We’ve learned how to find the first non-repeated character in a string using Java. This logic is commonly used in interviews and string-based challenges.

๐Ÿ” Master Java String & Frequency-Based Problems

Finding the first non-repeated character is a common Java interview and coding-round problem. Strengthen your string manipulation and HashMap-based logic by exploring these closely related problems and interview resources.

๐Ÿง  Java Coding Round Questions

Practice frequently asked string and HashMap-based problems.

๐Ÿงต Java String Pool & Interning

Understand how strings are stored and why immutability matters.

๐Ÿ“ฆ Java Collections Interview Questions

Learn how HashMap and LinkedHashMap are used in real interview problems.

๐Ÿ”„ Palindrome String Program

Another classic string comparison problem asked in interviews.

๐Ÿ” Reverse String in Java

Strengthen your understanding of string traversal techniques.

๐Ÿ”ข String Contains Only Digits

Practice validation-based string processing problems.

๐Ÿงฎ Check Anagram Strings

Learn character frequency comparison using arrays and maps.

๐Ÿ“ˆ Character Occurrence Count

Understand frequency counting as a foundation for many string problems.

๐ŸŽฏ Java Interview Questions (Freshers)

Common interview questions involving strings and collections.

๐Ÿงช Advanced Java Programs (Real-World)

Apply string processing logic in real-world Java scenarios.