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
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Swap Two Strings Without Using Third Variable
Comments
Post a Comment
Leave Comment