Second most repeated string in a sequence | Practice | GeeksforGeeks

Given a sequence of strings, the task is to find out the second most repeated (or frequent) string in the given sequence.
Note: No two strings are the second most repeated, there will be always a single string.
Example 1:
Input:
N = 6
arr[] = {aaa, bbb, ccc, bbb, aaa, aaa}
Output: bbb
Explanation: "bbb" is the second most 
occurring string with frequency 2.


â€‹Example 2:
Input: 
N = 6
arr[] = {geek, for, geek, for, geek, aaa}
Output: for
Explanation: "for" is the second most
occurring string with frequency 2.


Your Task:
You don't need to read input or print anything. Your task is to complete the function secFrequent() which takes the string array arr[] and its size N as inputs and returns the second most frequent string in the array.

Expected Time Complexity: O(N*max(|Si|).
Expected Auxiliary Space: O(N*max(|Si|).

Constraints:
1<=N<=103
 
0
bhavinraichura281 week ago
string secFrequent (string arr[], int n)   {       //code here.             int r1=0,r2=0,count =1;                     sort(arr,arr+n);       string s1=arr[0],s2;       for (int i=1;i<n;i++){           if ((arr[i]!=arr[i-1])||(i==n-1)){               if (i==n-1){                   count++;               }               if(r1<count && s1!=arr[i]){                   r2=r1;                   s2=s1;                   r1=count;                   s1=arr[i-1];                                  }               else if(r2<count && s1!=arr[i]){                                      r2=count;                   s2=arr[i-1];                                  }               count=0;                          }           count++;                  }       return s2;          }
0
officialshivaji0071 month ago
 public:   string secFrequent (string arr[], int n)   {       //code here.       map<string,int> mp;       for(int i=0;i<n;i++){           mp[arr[i]]++;       }              int mx = INT_MIN,mx2=INT_MIN;              for(auto x:mp){           mx = max(mx,x.second);       }       string ans ;       for(auto x:mp){           if(x.second>mx2 && x.second < mx){               mx2 = x.second;               ans = x.first;           }       }              return ans;   }
+1
sachinnnnnn1 month ago
public: bool static mycmp(pair<string,int> &a,pair<string,int> &b){       return a.second>b.second; }   string secFrequent (string arr[], int n)   {       unordered_map<string,int> m;       for(int i=0;i<n;i++){           m[arr[i]]++;       }       vector<pair<string,int>>v;       for(auto &a : m){           v.push_back(a);       }       sort(v.begin(),v.end(),mycmp);       return v[1].first;   }
0
sudeepgarg6711 month ago
#User function Template for python3class Solution:   def secFrequent(self, arr, n):       arr1 = []       arr2 = set(arr)       for i in arr2 :           arr1.append(arr.count(i))       arr1.remove(max(arr1))       for j in arr2 :           if arr.count(j) == max(arr1):               return j
0
bikrantsarmah2 months ago
string secFrequent (string arr[], int n)   {       //code here.              map<string,int> u;       int i;       for(i=0;i<n;i++)       u[arr[i]]++;              string s="";int m=INT_MIN;              for(auto x:u)       {           if(x.second>m)           {               s=x.first;               m=x.second;           }       }              u[s]=INT_MIN;              s=""; m=INT_MIN;               for(auto x:u)       {           if(x.second>m)           {               s=x.first;               m=x.second;           }       }              return s;                                             }
+1
kashyapjhon2 months ago
C++ Solution (Mirror Map Concept) Time=0.0/1.1 :
string secFrequent (string arr[], int n)   {       //code here.       string ans;       if(n==1 || n==2){           return ans;       }       unordered_map<string,int> u1;       unordered_map<int,string> u2;       vector<int> v;       for(int i=0;i<n;i++){           u1[arr[i]]++;       }       // int m=INT_MIN;       for(auto it= u1.begin();it!=u1.end();it++){           v.push_back(it->second);       }       sort(v.begin(),v.end());       for(auto it= u1.begin();it!=u1.end();it++){           u2.insert({it->second,it->first});       }       int N=v.size();       int t=v[N-2];       for(auto it= u2.begin();it!=u2.end();it++){           auto dc= u2.find(t);           return dc->second;       }   }
+1
aloksinghbais022 months ago
C++ solution having time complexity as O(k*log(k)) and space complexity as O(4*k) where k is total number of distinct strings in arr[] is as follows :- 
 
Execution Time :- 0.0 / 1.1 sec
 
string secFrequent (string arr[], int n){       unordered_map<string,bool> vis;       unordered_map<int,string> mp;       unordered_map<string,int> freq;       int code = 1;       for(int i = 0; i < n; i++){           if(!vis[arr[i]]){               vis[arr[i]] = true;               mp[code++] = arr[i]; // code to string           }           freq[arr[i]]++; // frequency of string       }              map<int,string> mp1;              for(auto p: mp){           mp1[freq[p.second]] = p.second;       }              int firstMost = 0,secondMost = 0;       string fm{},sm{};       for(auto p: mp1){           string str = p.second;           int f = p.first;           if(secondMost < firstMost){               sm = fm;               secondMost = firstMost;           }           if(firstMost < f){               fm = str;               firstMost = f;           }       }              return (sm);   }
0
khunalshaik2 months ago
    def secFrequent(self, arr, n):
       
       res = collections.Counter(arr)
       
       out = sorted(res.values())
       
       for k, v in res.items():
           
           if v == out[-2]:
               return k

0
manish14092 months ago
O(n) c++ easy solution
 
string secutil(unordered_map<string,int>&mp){      string ans;       int freq=0;       for(auto it= mp.begin();it!=mp.end();it++){           if(freq< it->second){               freq=it->second;               ans=it->first;           }       }       return ans; }    string secFrequent (string arr[], int n)   {       //code here.       unordered_map<string,int>mp;//to store frequency of word       for(int i=0;i<n;i++){           mp[arr[i]]++;       }       string temp= secutil(mp);//secutil function give then  freq of largest string       mp.erase(temp);//erase largest freq string       string ans=secutil(mp);//then first largest        return ans;         }
+1
anshulgupta966262 months ago
		map<string, int> mp;
        for(int i=0 ; i<n ; i++){
            mp[arr[i]]++;
        }
        vector<pair<int, string>> ans;
        map<string, int> :: iterator it;
        for(it = mp.begin() ; it != mp.end() ; it++){
            ans.push_back({it->second, it->first});
        }
        sort(ans.begin(), ans.end());
        int size = ans.size();
        return ans[size-2].second;
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.