Javatpoint Logo
Javatpoint Logo

Java Program to Find First Non-Repeating Character in String

Initially, there are many methods and logic to find the first non-repeating character in a string, and it just needs implementation. For implementation, we need to understand the logic, and we need to have a full grip on the programming language. Before implementing the logic using Java, we first need to define the Java programming definition.

Example for First Non-Repeating Character in String:

Consider an example for the question, take the word Javatpoint; start from the beginning of the string "Javatpoint." Examine each character one by one and keep track of the count of each character encountered. For all, the character count is one except for the character 'a' and 't,' for which the count is two. Now, scan the string again from the beginning and find the first character with a count of 1. In this case, it is 'j.'

Therefore, the first non-repeating character in the string "Javatpoint" is 'J.'

Algorithm:

The bellow is an algorithm for the above example:

STEP 1: START

STEP 2: Initialize firstNonRepeatingChar as the null character ('\0') and foundNonRepeating as false.

STEP 3: Iterate through each character in the input string using a loop.

STEP 4: For each character, check if it is repeated by iterating through the input string again and comparing it with other characters.

STEP 5: If the current character is not repeated, assign it to firstNonRepeatingChar, set foundNonRepeating to true, and break out of the loop.

STEP 6: After the loop, check the value of foundNonRepeating.

STEP 7: If foundNonRepeating is true, print "First non-repeating character is: " followed by firstNonRepeatingChar.

STEP 8: If foundNonRepeating is false, print "There is no non-repeating character in the input string."

STEP 9: END.

Java program using indexOf() and lastIndexOf():

NonRepeatingChar.Java

Output:

Input a String: JavaTpoint
The first character which is not repeating is: J

Time Complexity:

The time complexity of the provided program is O(n^2), where n is the length of the input string.

Java Program Using ASCII Values:

NonRepeatingChar.Java

Output:

Enter a String: JavaTpoint
The first character which is not repeating is: J

Time Complexity:

The time complexity of the provided program is O(n), where n is the length of the input string.

Java Program Using Brute Force Approach:

NonRepeatingChar.Java

Output:

Enter a String: JavaTpoint
The first character which is not repeating is: J

Time Complexity:

The time complexity of the provided program is O(n^2), where n is the length of the input string.

Java Program Using Sets and Lists:

NonRepeatingChar.Java

Output:

Enter a String: JavaTpoint
The first character which is not repeating is: J

Time Complexity:

The time complexity of the provided program is O(n^2), where n is the length of the input string.

Conclusion:

Hence the above are the different approaches for the given query, i.e., to write a Java program to find the first non-repeating character in a string. Hence the above four programs will end up in successful execution without errors. Hence the logic can be understood by tracing the programs by giving your own outputs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA