remove duplicates from string

pull/1012/head
Depika Gupta 2022-10-11 17:47:13 +05:30 committed by GitHub
parent 04d42af7c0
commit 98ad8024ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
// https://practice.geeksforgeeks.org/problems/remove-duplicates3034/1
/* Given a string without spaces, the task is to remove duplicates from it.*/
//User function template for C++
class Solution {
public:
string removeDups(string S)
{
if (S == " " || S == "")
return S;
unordered_map<char, int>mp;
int len = S.size();
for (int i = 0; i < len; i++)
{
mp[S[i]]++;
}
string result = "";
for (int i = 0; i < len; i++)
{
if (mp[S[i]] != -1)
{
result += S[i];
mp[S[i]] = -1;
}
}
return result;
}
};