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
choubey4411119 hours ago
class Solution {   String removeDups(String S) {       // code here       LinkedHashMap<Character,Integer> set = new LinkedHashMap<>();       for(int i=0;i<S.length();i++){           char c = S.charAt(i);           set.put(c,set.getOrDefault(c,0)+1);       }
       String n ="";       // Set<Entry<Character,Integer>> entryset = map.entrySet();       for(Map.Entry<Character, Integer> entry : set.entrySet()){           n = n + entry.getKey();       }       return n;   }}
0
abhijeetraj32 days ago
//0.19 seconds Javascriptlet output = '';   (new Set(str)).forEach((element) => {       output += element;   });   return output;
0
vanshpratik01655 days ago
 string str ="";    vector<char> s;    for(int i=0;i<S.size();i++)    {        if(count(s.begin(),s.end(),S[i])) continue;        else s.push_back(S[i]);    }    for(auto a:s)    {        str+=a;    }    return str;
0
codewithshoaib191 week ago
 String removeDups(String S) {       // code here       String ans="";       for(int i=0;i<S.length();i++){           if(!ans.contains(S.substring(i,i+1))){               ans=ans+S.substring(i,i+1);           }       }       return ans;   }
0
rajanithinbatchu1 week ago
def removeDups(self, S):
		a=''
		for i in S:
		    if i in a:
		        continue
		    else:
		        a+=i
		return a
0
subhaduleygba1 week ago
//c++ code 
 string removeDups(string S) {    // Your code goes here    string ans="";     for(int i=0;i<S.size();i++){         if(ans.find(S[i])== string::npos){             ans=ans+S[i];         }     }       return ans; } 
0
mayank180919992 weeks ago
	string removeDups(string S) 
	{
	    // Your code goes here
	unordered_map<char,int>m;
    string ans;
    for(int i=0;i<S.size();i++){
        if(m.find(S[i])==m.end()){
            ans.push_back(S[i]);
            m[S[i]]++;
        }
    }
      return ans;
	}
-1
sanchitkhatar332 weeks ago
C++ Simple Hashmap approach
TC = O(n)
SC = O(n) [worst case]
upvote if you like!
string removeDups(string s) 
	{
	    // Your code goes here
	    unordered_map<char,int>um;
	    string ans;
	    for(int i = 0;i<s.size();i++){
	        if(um.find(s[i]) == um.end()){
	            ans.push_back(s[i]);
	            um[s[i]]++;
	        }
	    }
	    return ans;
	}
+1
rajatgupta96962 weeks ago
//C++ Code
string removeDups(string S) 
	{
	    string ans="";
	    // Your code goes here
	    int hash[26]={0};
	    for(int i=0;i<S.length();i++)
	        hash[S[i]-'a']++;
	    
	    for(int i=0;i<S.length();i++)
	    {
	        if(hash[S[i]-'a']>=1){
	            ans+=S[i];
	            hash[S[i]-'a']=0;
	        }    
	    }
	    return ans;
	}
0
kkumarakash70112 weeks ago
class Solution {   String removeDups(String S) {       // code here       int a[]=new int[256];       for(int i=0;i<S.length();i++)       {           a[S.charAt(i)]=a[S.charAt(i)]+1;       }              String s="";       for(int i=0;i<S.length();i++)       {           if(a[S.charAt(i)]>=1)           {               s+=S.charAt(i);               a[S.charAt(i)]=0;           }       }       return s;   }}
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.