Remove all duplicates from a given string | Practice | GeeksforGeeks

Given a string Str which may contains lowercase and uppercase chracters. The task is to remove all duplicate characters from the string and find the resultant string. The order of remaining characters in the output should be same as in the original string.
Example 1:
Input:
Str = geeksforgeeks
Output: geksfor
Explanation: After removing duplicate
characters such as e, k, g, s, we have
string as "geksfor".

Example 2:
Input:
Str = HappyNewYear
Output: HapyNewYr
Explanation: After removing duplicate
characters such as p, e, a, we have
string as "HapyNewYr".

Your Task:
Complete the function removeDuplicates() which takes a string str, as input parameters and returns a string denoting the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 105
String contains uppercase and lowercase english letters.
 
0
mayank18091999in 9 hours
	string removeDuplicates(string str) {
	    // code here
	    unordered_map<char,int>m;
	    string v="";
	    for(int i=0;i<str.length();i++){
	        m[str[i]]++;
	        if(m[str[i]]==1){
	            v.push_back(str[i]);
	        }
	    }
	   return v;
	}
0
singhvipnesh6in 3 hours
class Solution {   String removeDuplicates(String str) {       HashMap<Character,Integer>map=new HashMap<>();        String s="";       for(int i=0;i<str.length();i++)       {           char ch=str.charAt(i);          if(!map.containsKey(ch)){          map.put(ch,1);          s+=ch;          }          else          map.put(ch,map.get(ch)+1);       }      return s;   }}
 
0
codewithshoaib192 days ago
// Easy Java solution 100% Accuracy 
String removeDuplicates(String str) {       // code here       String ans="";       for(int i=0;i<str.length();i++){           String a=str.substring(i,i+1);           if(!ans.contains(a)){               ans=ans+a;           }       }       return ans;   }
0
rajanithinbatchu6 days ago
def removeDuplicates(self,str):
	    s=''
	    for i in str:
	        if i in s:
	            continue
	        else:
	            s+=i
	    return s
+1
anishmeena0011 week ago
using set
string removeDuplicates(string str) {    // code here    unordered_set <char> s;    string st="";    for(int i=0;i<str.length();i++)    {        if(s.find(str[i])==s.end())        {           s.insert(str[i]);            st+=str[i];        }    }    return st;}
+1
parmeet4501 week ago
String removeDuplicates(String str) {       // code here       ArrayList<Character> al = new ArrayList<Character>();       for(int i=0;i<str.length();i++){           char ch = str.charAt(i);           if(!al.contains(ch)){               al.add(ch);           }       }       str="";       for(char ch : al){           str+=ch;       }
       return str;   }
+1
kartikeyashokgautam2 weeks ago
JAVA Solutions :-
 
    String removeDuplicates(String str) {
              String s ="";           HashSet<Character> hs = new HashSet<>();        for(int i=0;i<str.length();i++)    {                if(!hs.contains(str.charAt(i)))        {            hs.add(str.charAt(i));            s+=str.charAt(i);        }          }       return s;}
 
0
csedeepak2 weeks ago
easy cpp solution:-
int count[256]={0};      string ans = "";    for(int i=0; i<str.length(); i++)         count[str[i]]++;    for(int i=0; i<str.length(); i++){        if(count[str[i]]>=1){           ans += str[i];           count[str[i]]=0;        }    }    return ans;
0
krjjain143 weeks ago
 def removeDuplicates(self,str):    # code here    out = []    for i in str:        if i not in out:            out.append(i)    return "".join(out)
0
balajibvrit4 weeks ago
  string s;    for(int i=0; i<str.size();i++)    {        if(s.find(str[i])==-1)        s.push_back(str[i]);    }    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.