I wrote the following code for removing the duplicates from a given string i.e. if ARRUN is the input then the output will be ARUN.
#include <bits/stdc++.h>
using namespace std;
char* removeDuplicates(string &s,int n){
char arr[n];
unordered_map<char,int> exists;
int index = 0;
for(int i=0;i<n;i++){
if(exists[s[i]]==0)
{
arr[index++] = s[i];
exists[s[i]]++;
}
}
return arr;
}
//driver code
int main(){
string str;
cin >> str;
cout<<removeDuplicates(str,str.length())<<endl;
return 0;
}
The code produces no output at all, however, it works fine if I use char arr[] instead of string class.
#include <bits/stdc++.h>. It is not just bad practice - it is not proper C++. It ruins portability and fosters terrible habits. By using it, you not only grant the compiler the right to break your code at any time without notice but also renders your code non-portable and unprofessional. It also creates implicit dependency on any future facility of the C++ standard library, thus basically screwing up compile time. See Why should I not#include <bits/stdc++.h>.