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
hrishikeshwagh2001in 5 hours
 string removeDuplicates(string str) {    string res = "";    unordered_map<char,int>mp;    for(int i=0;i<str.length();i++){        if(mp[str[i]]>0)            continue;                res.push_back(str[i]);        mp[str[i]]++;    }    return res;}
0
dilipchoudhary7272 hours ago
class Solution {
    String removeDuplicates(String str) 
    {
        String ans="";
       for(int i=0;i<str.length();i++)
       {
           if(!ans.contains(String.valueOf(str.charAt(i))))
           {
               ans+=str.charAt(i);
           }
       }
       return ans;
    }
}

0
satyammauryaksm7112 days ago
 string removeDuplicates(string str) {    string result="";        int hash[256]={0};    for(int i=0;i<str.length();i++){    if(hash[str[i]]==0){        hash[str[i]]=1;        result = result + str[i];            }            }    return result;}
0
sabtharishiPremium4 days ago
 
PYTHON USING HASH MAPS.
 
class Solution:
	def removeDuplicates(self,str):
	    hashdict = {}
	    result = ""
	    for ch in str:
	        if ch not in hashdict.keys():
	            hashdict[ch]=1
	            result+=ch
	        else:
	            pass
	    return result
0
sabtharishi
This comment was deleted.
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.