Non Repeating Character | Practice | GeeksforGeeks

Given a string S consisting of lowercase Latin Letters. Find the first non-repeating character in S.
Example 1:
Input:
S = hello
Output: h
Explanation: In the given string, the
first character which is non-repeating
is h, as it appears first and there is
no other 'h' in the string.
Example 2:
Input:
S = zxvczbtxyzvy
Output: c
Explanation: In the given string, 'c' is
the character which is non-repeating. 

Your Task:
You only need to complete the function nonrepeatingCharacter() that takes string S as a parameter and returns the character. If there is no non-repeating character then return '$' .
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Number of distinct characters).
Note: N = |S|
Constraints:
1 <= N <= 103
 
0
codewithshoaib193 days ago
static char nonrepeatingCharacter(String S)   {       //Your code here       int[]freq=new int[26];      for(int i=0;i<S.length();i++){          freq[S.charAt(i)-'a']++;      }      for(int i=0;i<S.length();i++){          int x=freq[S.charAt(i)-'a'];          if(x==1){              return S.charAt(i);              }      }      return '$';   }
0
mayank180919991 week ago
 char nonrepeatingCharacter(string S)
    {
       //Your code here
       unordered_map<char,int>m;
       for(int i=0;i<S.length();i++){
           m[S[i]]++;
       }
       for(int i=0;i<S.length();i++){
           if(m[S[i]]==1){
               return S[i];
           }
       }
      return '$';
    }
+1
rajatgupta96961 week ago
 int arr[26]={0};
       for(auto  x : S)
            arr[x-'a']++;  
            // store character count in hash array
        
        for(int i=0;i<S.length();i++)
        {
            if(arr[S[i]-'a']==1) 
            // if character count is 1 from left side of string
                return S[i];
        }
        return '$';
0
imtarun_chaubey1 week ago
class Solution{   public:   //Function to find the first non-repeating character in a string.   char nonrepeatingCharacter(string S)   {      //Your code here      int arr[26] = {0};      for(auto x:S){          arr[x-'a']++;      }            for(auto x:S){          if(arr[x-'a'] == 1) return x;      }       return '$';   }
};
0
sunghunet2 weeks ago
import collections

class Solution:
    
    def nonrepeatingCharacter(self,s):
        
        l = list(s)
        t = collections.Counter(l)
        
        for v in l:
            if v in t and t[v] == 1:
                return v;
                
        return '$'
+1
ramendramishra1972ra3 weeks ago
class Solution{// using o(1) auxillary space and o(n)time   public:   //Function to find the first non-repeating character in a string.   char nonrepeatingCharacter(string S)   {  vector<int>v(256,0);     for(int i=0;i<S.length();i++){       v[S[i]]++;          }   for(int i =0;i<S.length();i++){       if(v[S[i]]==1){           return S[i];       }   }   // Your code herereturn '$'; 
0
swapniltayal4223 weeks ago
class Solution:      #Function to find the first non-repeating character in a string.   def nonrepeatingCharacter(self,s):       #code here       d = dict()       for i in s:           d[i] = d.get(i, 0) + 1       for i in s:           if (d[i] == 1):               return i                      return -1
0
ravikant021113 weeks ago
//Function to find the first non-repeating character in a string.   static char nonrepeatingCharacter(String S)   {       //Your code here       int count[] = new int[26];       for(int i = 0; i < S.length(); i++){           char ch = S.charAt(i);           count[ch-'a']++;       }              for(int i = 0; i < S.length(); i++){            char ch = S.charAt(i);           if(count[ch-'a']==1){               return ch;           }       }              return '$';   }
 
0
adityagagtiwari3 weeks ago
Easy solution O(26) space and O(n) time.
   static char nonrepeatingCharacter(String S)   {       //Your code here        int[] count = new int[26];                for(char ch:S.toCharArray())        {            count[ch-'a']++;        }           for(char ch:S.toCharArray())        {            if(count[ch-'a']==1)            {                return ch;            }        }        return '$';   }
0
jayesh293 weeks ago
Easy JAVA Solution using HashMap :-
    static char nonrepeatingCharacter(String str){
        HashMap<Character,Integer> map = new HashMap<>();
        for(int i=0;i<str.length();i++){
            map.put(str.charAt(i),map.getOrDefault(str.charAt(i),0)+1);
        }
        for(int i=0;i<str.length();i++){
            if(map.get(str.charAt(i))==1) return str.charAt(i);
        }
       System.out.print("-");
       return '1';
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.