Remove Duplicates | Practice | GeeksforGeeks

Given a string without spaces, the task is to remove duplicates from it. 
Note: The original order of characters must be kept the same. 
Example 1:
Input: S = "zvvo"
Output: "zvo"
Explanation: Only keep the first
occurrence
Example 2:
Input: S = "gfg"
Output: gf
Explanation: Only keep the first
occurrence
Your task:
Your task is to complete the function removeDups() which takes a single string as input and returns the string. You need not take any input or print anything.
 
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(constant)
Constraints:
1 <= |S| <= 105
S conatins lowercase english alphabets
0
vijeayaetv0in 12 hours
Python:
 
a="" l=[] for i in S:     if i not in l:         a+=i         l.append(i) return a
0
vinayaknagarimscit10 hours ago
#python
 rep=[] listString=list(S)  for i in listString:     if i not in rep:         rep.append(i) return ("".join(rep))
+1
aditiwadatkar2 days ago
C++  Using bits to make hash table and performing bitwise operations -
string removeDups(string S) 
	{
	    // Your code goes here
	    long int H=0,x=0;
	    string ans;
	    int i=0;
	    while(S[i]!='\0')
	    {
	        x=1;
	        x=x<<S[i]-97;
	        if((x&H) > 0)
	        {
	           i++;   
	        }
	        else
	        {
	          H=(x|H);
	          ans=ans+S[i];
	          i++;  
	        }
	    }
	    return ans;
	}
0
noobslay4ad3 days ago
Using Python:- 
class Solution:def removeDups(self, S):    k=''    for i in range(len(S)-1,-1,-1):               if i==0:            k=S[i]+k            break        if(S.find(S[i], 0, i)==-1):            k=S[i]+k                                  return k
0
vaishnavinavale01014 days ago
 JAVA SOLUTION:
class Solution {   String removeDups(String S) {       // code here       HashSet<Character> set= new HashSet<>();       StringBuilder ans =new StringBuilder();       for(int i=0;i<S.length();i++)       {           if(!set.contains(S.charAt(i)))           {               ans.append(S.charAt(i));           }           set.add(S.charAt(i));       }       return ans.toString();   }}
0
skumarsahu55454 days ago
 string removeDups(string S) {    // Your code goes here    map<char, int> mp;    string ans = "";    for(int i=0; i<S.length(); i++){        if(mp[S[i]] == 0){            ans +=S[i];        }        mp[S[i]]++;    }    return ans;}
0
yakash2220006 days ago
class Solution {   String removeDups(String S) {       int[] arr =new int[26];       String res="";       for(char ch: S.toCharArray()){           if(arr[ch-'a']==1)           continue;           arr[ch-'a']=1;           res+=ch;                  }       return res;   }}
0
luvgupta76691 week ago
Simple Working Best Java Solution (Without additional collection usage)-
 
        String res="";
        for(int i=0; i<S.length(); i++){
            if(res.indexOf(S.charAt(i)) == -1)
              res += S.charAt(i);
        }
        return res;
0
2020pgcaca901 week ago
 string removeDups(string S) {    // Your code goes here    map<char,int> m;    string ans="";    for(int i=0;i<S.length();i++)    {        if(m[S[i]]==0)        ans+=S[i];                m[S[i]]++;    }    return ans;}
0
mbi20210121 week ago
string removeDups(string s) {    if(s.size()<=1)return s;    unordered_map<char,int>mp;    string ans="";    for(int i=0;i<s.size();i++)    {        if(mp.find(s[i])==mp.end())        {            ans+=s[i];            mp[s[i]]++;        }            }    return ans;}
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.