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

Introduction

Finding the first non-repeated character in a string is a common problem in text processing and interviews. It helps you understand string manipulation, data structures like maps, and the importance of efficient algorithms in Java. This guide will walk you through writing a Java program that finds the first non-repeated character in a given string.

Problem Statement

Create a Java program that:

  • Prompts the user to enter a string.
  • Finds the first character in the string that does not repeat.
  • Displays the first non-repeated character.

Example:

  • Input: "swiss"
  • Output: First non-repeated character: w

Solution Steps

  1. Read the String: Use the Scanner class to take the string as input from the user.
  2. Store Character Counts: Use a LinkedHashMap to store the characters of the string as keys and their counts as values.
  3. Identify the First Non-repeated Character: Iterate through the map to find the first character with a count of 1.
  4. Display the Character: Print the first non-repeated character.

Java Program

// Java Program to Find the First Non-repeated Character in a String // Author: https://www.rameshfadatare.com/ import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class FirstNonRepeatedCharacter { public static void main(String[] args) { // Step 1: Read the string from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter a string: "); String input = scanner.nextLine(); // Step 2: Store character counts Map<Character, Integer> charCountMap = new LinkedHashMap<>(); for (char ch : input.toCharArray()) { charCountMap.put(ch, charCountMap.getOrDefault(ch, 0) + 1); } // Step 3: Identify the first non-repeated character char firstNonRepeatedChar = '\0'; for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) { if (entry.getValue() == 1) { firstNonRepeatedChar = entry.getKey(); break; } } // Step 4: Display the first non-repeated character if (firstNonRepeatedChar != '\0') { System.out.println("First non-repeated character: " + firstNonRepeatedChar); } else { System.out.println("No non-repeated character found."); } } } 

Explanation

Step 1: Read the String

  • The Scanner class is used to read a string input from the user. The nextLine() method captures the entire line as a string.

Step 2: Store Character Counts

  • A LinkedHashMap is used to store the characters as keys and their counts as values. This ensures that the characters are stored in the order they appear in the string.

Step 3: Identify the First Non-repeated Character

  • The program iterates through the entries of the LinkedHashMap to find the first character that has a count of 1. This is the first non-repeated character.

Step 4: Display the First Non-repeated Character

  • The program checks if a non-repeated character was found and displays it. If no such character is found, it prints a message indicating that.

Output Example

Example:

Enter a string: swiss First non-repeated character: w 

Conclusion

This Java program demonstrates how to find the first non-repeated character in a user-input string. It covers important concepts such as string manipulation, using maps to store character counts, and iterating through collections, making it a useful exercise for beginners learning Java programming.

Leave a Comment

Scroll to Top