Wednesday, December 6, 2017

Maximum repeated Character - CTS2

Find the character which appeared the maximum time. If you have multiple character as result return the first character in that list. 

Sample Input & Output:
Input: "helloworld"
Output: 'l'
Input: "yehaha"
Output: 'h

Note: Don’t use scanf, cin or Scanner class. Use command line arguments to get inputs

Solution:
#include<iostream>
#include <map>
using namespace std;
int main(int x,char *v[])
{
 string t,s=v[1];
 map<char,int> m;
 int c=0;
 char ch;
 for(int i=0;i<s.size();i++)
  if(++m[s[i]]==1)
   t+=s[i];
 for(int i=0;i<t.size();i++)
  if(c<m[t[i]])
  {
   c=m[t[i]];
   ch=t[i];
  }
  cout<<ch;
}


Share: