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

This is the Java Program to Find the First non-repeated Character in a String

Problem Description

Given a string of characters, find the first non-repeated character in the string.

Example:

String x = “Hi this is Marry”
Output = ‘H’

Problem Solution

Make two new arrays to store the frequency of each character and the indexes of there occurrences. Iterate through the string to fill the two arrays. Finally, re-iterate through the new arrays, to find the index of the minimum index of the characters whose frequency are 1.

Program/Source Code

Here is the source code of the Java Program to Find the First non-repeated Character in a String. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

advertisement
advertisement
  1.  
  2. // Java Program to Find the First non-repeated Character in a String.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7.  
  8. public class NonRepeatedCharacter {
  9.     // Function to find the first non-repeating character
  10.     static int firstNonRepeatingCharacter(String str){
  11.         int[] arrayCount = new int[256];
  12.         int[] arrayIndex = new int[256];
  13.         int i;
  14.         for(i=0; i<str.length(); i++){
  15.             arrayCount[str.charAt(i)]++;
  16.             arrayIndex[str.charAt(i)] = i;
  17.         }
  18.         int index = Integer.MAX_VALUE;
  19.         for(i=0; i<256; i++){
  20.             if(arrayCount[i] == 1 && arrayIndex[i] < index)
  21.                 index = arrayIndex[i];
  22.         }
  23.         return index;
  24.     }
  25.     // Function to read user input
  26.     public static void main(String[] args) {
  27.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  28.         String str;
  29.         System.out.println("Enter the string");
  30.         try {
  31.             str = br.readLine();
  32.         } catch (IOException e) {
  33.             System.out.println("An I/O error occurred");
  34.             return;
  35.         }
  36.         int index = firstNonRepeatingCharacter(str);
  37.         if(index < str.length())
  38.             System.out.println("First Non Repeating Character is "
  39.                                               + str.charAt(index));
  40.         else
  41.             System.out.println("Each character is repeated");
  42.     }
  43. }
Program Explanation

1. In function firstNonRepeatingCharacter(), two arrays arrayCount and arrayIndex are created.
2. The loop for(i=0; i<str.length(); i++) iterates through the string and counts the frequency of each character in arrayCount array and the index of the last occurence of a character in arrayIndex array.
3. The loop for(i=0; i<256; i++) iterates through the two arrays arrayCount and arrayIndex.
4. The condition if(arrayCount[i] == 1 && arrayIndex[i] < index) checks if the frequency of character is 1 and the second part looks for first non-repeating character.

Time Complexity: O(n) where n is the length of the string.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
Case 1 (Positive Test Case):
 
Enter the string
Hi this is Marry
First Non-Repeating Character is H
 
Case 2 (Positive Test Case - another example):
 
Enter the string
well well finally you are here
First Non-Repeating Character is f
 
Case 3 (Negative Test Case):
 
Enter the string
WellWell
Each character is repeated

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.