In this post we will learn How to find first non-repeated character in a given String in Java with/without using Java inbuilt API or data structure.
We will look three approaches to solve this problem statement.
- Without using any in-built API or data structure
- Using LinkedHashMap data structure
- using String class indexOf(int ch) and lastIndexOf(int ch) methods
Approach 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * This java program is all about how to find first Non repeated character in a given input String */ public class ClientTest1 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Taking input from keyboard using Scanner System.out.println("Enter String to find first non-repeated character::"); String inputText = scanner.next(); Character c = findFirstNonRepeatedCharacterFromString(inputText); if(c !=null) System.out.println("First Non Repeated character is: " + c); else System.out.println("There is no single Non Repeated character found"); } catch (Exception e) { e.printStackTrace(); } } private static Character findFirstNonRepeatedCharacterFromString(String inputText) { // base case if input is null or empty if (inputText == null || inputText.trim().isEmpty()) { System.out.println("Enter valid input text.."); return null; } boolean isNonRepeatedCharFound = false; for (int i = 0; i < inputText.length(); i++) { isNonRepeatedCharFound = true; char c = inputText.charAt(i); for (int j = 0; j < inputText.length(); j++) { /* * if we find matching character then set the boolean field to * false we also skip the char which is already compared */ if (c == inputText.charAt(j) && i != j) { isNonRepeatedCharFound = false; break; } } if (isNonRepeatedCharFound) { return c; } } if (!isNonRepeatedCharFound) { return null; } return null; } } |
Approach 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.kkjavatutorials.client; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; import java.util.Map.Entry; /** * @author KK JavaTutorials * This java program is all about how to find first Non repeated character in a given input String */ public class ClientTest2 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Taking input from keyboard using Scanner System.out.println("Enter String to find first non-repeated character::"); String inputText = scanner.next(); Character c = findFirstNonRepeatedCharacterFromString(inputText); if(c != null) System.out.println("First Non Repeated character is: " + c); else System.out.println("There is no single Non Repeated character found"); } catch (Exception e) { e.printStackTrace(); } } private static Character findFirstNonRepeatedCharacterFromString(String inputText) { // base case if input is null or empty if (inputText == null || inputText.trim().isEmpty()) { System.out.println("Enter valid input text.."); return null; } //LinkedHashMap always maintains the insertion order that the region to choose this data structure Map<Character, Integer> charCountMap = new LinkedHashMap<Character, Integer>(); for (int i = 0; i < inputText.length(); i++) { Character ch = inputText.charAt(i); // If character found in map increment the count if (charCountMap.containsKey(ch)) { charCountMap.put(ch, charCountMap.get(ch) + 1); } else { //Initialize the count by 1 first time charCountMap.put(ch, 1); } } //Iterate the above map and find the first character with count value 1 for (Entry<Character, Integer> entry : charCountMap.entrySet()) { if (entry.getValue() == 1) { return entry.getKey(); } } return null; } } |
Approach 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * This java program is all about how to find first Non repeated character in a given input String */ public class ClientTest3 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Taking input from keyboard using Scanner System.out.println("Enter String to find first non-repeated character::"); String inputText = scanner.next(); Character c = findFirstNonRepeatedCharacterFromString(inputText); if(c != null) System.out.println("First Non Repeated character is: " + c); else System.out.println("There is no single Non Repeated character found"); } catch (Exception e) { e.printStackTrace(); } } /* * Here I have used logic- indexOf () method returns the index of the first * occurrence of a specified character where the lastIndexOf () method returns the * index of the last occurrence of the specified character. If both the indexes are equal * it means that character is only once in the input string. */ private static Character findFirstNonRepeatedCharacterFromString(String inputText) { // base case if input is null or empty if (inputText == null || inputText.trim().isEmpty()) { System.out.println("Enter valid input text.."); return null; } for (int i = 0; i < inputText.length(); i++) { Character ch = inputText.charAt(i); if (inputText.indexOf(ch) == inputText.lastIndexOf(ch)) { return ch; } } return null; } } |
I have checked all the above programs for few inputs as below. You may check for other input as well
1 2 3 4 5 6 7 8 9 10 11 |
Enter String to find first non-repeated character:: java First Non Repeated character is: j Enter String to find first non-repeated character:: tutorials First Non Repeated character is: u Enter String to find first non-repeated character:: KKJavaTutorials First Non Repeated character is: J |
That’s all about this topic How to find first non-repeated character in a given String in Java.
You May Also Like:
Reverse a string in Java (6 Different Ways)
Splitting a String in Java using a delimiter
Count number of words in a string java
Count total number of times each character appears in the string in java
Check if two strings are anagrams or not in java
How to convert string to int without using library functions in java
Check Whether a Given String/Number is a Palindrome in java
Java Program to find the frequency of each character in String ?
If you have any feedback or suggestion please feel free to drop in blow comment box.