Duplicate characters in a string and print them in order

#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<int,char> &a,
const pair<int,char> &b)
{
return (a.second < b.second);
}
int main()
{
//write your code here
int t;
cin>>t;

while(t–)
{
int chararr[256]={0};
string s;
cin>>s;
for(int i=0;i<s.length();i++)
{
chararr[s[i]]++;
}
vector<pair<int ,char> > v;
int k=0;
for(int i=0;i<s.length();i++)
{

  if(chararr[s[i]]>=2)
  {
    v.push_back(make_pair(chararr[s[i]],s[i]));
    k++;
  }
  chararr[s[i]]=0;
}
if(k==0)
{
  cout<<"-1"<<endl;
  break;
}
sort(v.begin(),v.end(),sortbysec);
for(int i=0;i<v.size();i++)
{
  cout<<v[i].second<<"="<<v[i].first<<" ";
}
cout<<endl;

}

return 0;
}

Simply, take a map<char,int> and if mp[ch]>1 print ch

@happy_man1 can you send me the function as i am not familiar with map

string s;
cin>>s;
map<char,int>mp;
for(int i=0;s[i];i++){
mp[s[i]]++;
}
for(auto it:mp){
if(it.second>1){
cout<<it.first<<" ";
}
}

1 Like

i also want to know is why my code is giving wrong answer Any help??

its not working!!i think you have solved for a different question

I have not put the -1 condition, moreover could you also post the example output
Edit- I printed output format wrong
print it as- it.first<<"="<<it.second

1 Like

tq so much @happy_man1

1 Like