Given a string str of length L, the task is to find the first occurrence of a non-repeating character in the string.
Examples:
Input: str = “geeksforgeeks”
Output: fInput: str = “programmer”
Output: p
Note: Please refer this article for HashMap approach and this article as space-optimized approach.
Linked List Approach: The idea is to use Linked List to keep track of the unique elements in the string. Below is the illustration of the approach:
- Iterate over the string for each character in the string and add the character in the Linked List on basis of the below conditions:
- If the character is already present in the Linked List, then remove the existing character node from the linked list.
- Otherwise, Add the character into the linked list.
- Finally, the character at the first node of the linked list is the first non-repeating character of the string.
Below is the implementation of the above approach:
Java
// Java implementation to find the // first non-repeating element // of the string using Linked List import java.util.LinkedList; public class FirstNonRepeatingElement { // Function to find the first // non-repeating element of the // given string using Linked List static void firstNonRepElement(String str) { LinkedList<Character> list = new LinkedList<Character>(); list.add(str.charAt(0)); for (int i = 1; i < str.length(); i++) { if (list.contains(str.charAt(i))) list.remove(list.indexOf( str.charAt(i))); else list.add(str.charAt(i)); } System.out.println(list.get(0)); } // Driver Code public static void main(String[] args) { String str = "geeksforgeeks"; // Function Call firstNonRepElement(str); } } |
f
Performance Analysis:
- Time Complexity: O(N * 26)
- Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the string formed by joining k consecutive nodes of linked list
- Maximum occurring character in a linked list
- Find a string such that every character is lexicographically greater than its immediate next character
- Find the character in first string that is present at minimum index in second string
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Find the last non repeating character in string
- Find k-th character of decrypted string | Set - 2
- Find k'th character of decrypted string | Set 1
- Find last index of a character in a string
- Find the first repeated character in a string
- Find one extra character in a string
- Given a string, find its first non-repeating character
- How to find the first and last character of a string in Java
- Find the Nth occurrence of a character in the given String
- Convert a String to a Singly Linked List
- Program to find the kth character after decrypting a string
- Find repeated character present first in a string
- Swap the vowels in the linked list representation of a string
- Reverse all the word in a String represented as a Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

