Open In App

Find first non-repeating character in a given string using Linked List

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: f

Input: 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:

Below is the implementation of the above approach: 




// C++ implementation to find the
// first non-repeating element of the
// string using Linked List
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the first
// non-repeating element of the
// given string using Linked List
void firstNonRepElement(string str)
{
    map<char, int> mpp;
 
    for(auto i:str)
    {
        mpp[i]++;
    }
 
    for(auto i:str)
    {
        if (mpp[i] == 1)
        {
            cout << i << endl;
            return;
        }
    }
    return;
}
 
// Driver Code
int main()
{
    string str = "geeksforgeeks";
 
    // Function Call
    firstNonRepElement(str);
}
 
// This code is contributed by mohit kumar 29




// 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);
    }
}




# Python3 implementation to find the
# first non-repeating element of the
# string using Linked List
import collections
 
# Function to find the first
# non-repeating element of the
# given string using Linked List
def firstNonRepElement(str):
     
    # Make list as a linkedlist
    list = collections.deque()# linkedlist
 
    list.append(str[0])
 
    for i in range(len(str)):
        if str[i] in list:
            list.remove(str[i])
        else:
            list.append(str[i])
             
    print(list[0])
 
# Driver Code
if __name__=='__main__':
     
    str = "geeksforgeeks";
     
    # Function Call
    firstNonRepElement(str);
 
# This code is contributed by pratham76




// C# implementation to find the
// first non-repeating element
// of the string using Linked List
using System;
using System.Collections;
using System.Collections.Generic;
 
class FirstNonRepeatingElement{
 
// Function to find the first
// non-repeating element of the
// given string using Linked List
static void firstNonRepElement(string str)
{
    LinkedList<char> list = new LinkedList<char>();
 
    list.AddLast(str[0]);
 
    for(int i = 1; i < str.Length; i++)
    {
        if (list.Contains(str[i]))
            list.Remove(str[i]);
        else
            list.AddLast(str[i]);
    }
    Console.Write(list.First.Value);
}
 
// Driver Code
public static void Main(string[] args)
{
    string str = "geeksforgeeks";
 
    // Function call
    firstNonRepElement(str);
}
}
 
// This code is contributed by rutvik_56




<script>
// Javascript implementation to find the
// first non-repeating element
// of the string using Linked List
 
    // Function to find the first
    // non-repeating element of the
    // given string using Linked List
function firstNonRepElement(str)
{
    let list = [];
  
        list.push(str[0]);
  
        for (let i = 1; i < str.length; i++)
        {
            if (list.includes(str[i]))
                list.splice(list.indexOf(str[i]),1);
            else
                list.push(str[i]);
             
        }
        document.write(list[0]);
}
 
// Driver Code
let str = "geeksforgeeks";
 
// Function Call
firstNonRepElement(str);
 
// This code is contributed by patel2127
</script>

Output
f

Performance Analysis:


Article Tags :