// C Program to Find second most frequent character in a String
#include <stdio.h>
#include <string.h>
int main()
{
char str[110],answer;
int i, length;
int max = -1;
int frequency[256] = {0};
printf("\nEnter the String of characters: ");
gets(str);
length = strlen(str);
for(i = 0; i < length; i++)
{
frequency[str[i]]++;
}
for(i = 0; i < length; i++)
{
if(max < frequency[str[i]])
{
max = freq[str[i]];
answer = str[i];
}
}
printf("\nThe second most frequent character in a Given String is= %c ", answer);
return 0;
}
Output
Enter the String of characters: aabbbcde
The second most frequent character in a Given String is= a
// Java Program to find the second most frequent character in a given string
import java.util.*;
public class Main
{
static final int CHARS = 256;
static char secondMostFreqChar(String str1)
{
int[] ch = new int[CHARS];
for (int i = 0; i < str1.length (); i++)
(ch[str1.charAt (i)])++;
int ch_first = 0, ch_second = 0;
for (int i = 0; i < CHARS; i++)
{
if (ch[i] > ch[ch_first])
{
ch_second = ch_first;
ch_first = i;
}
else if (ch[i] > ch[ch_second] && ch[i] != ch[ch_first])
ch_second = i;
}
return (char) ch_second;
}
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println ("Enter a string: ");
String str1=sc.next();
System.out.println ("The given string is: " + str1);
char res = secondMostFreqChar (str1);
if (res != '\0')
System.out.println ("The second most frequent character in the string is: " +res);
else
System.out.println ("No second most frequent character found in the string");
}
}
Output
Enter a string: visit prepinsta
The given string is: visit prepinsta
The second most frequent character in the string is: p
import java.util.*;
public class SecondMostFrequentCharcter {
static void secondFrequent(String s){
HashMapmap=new HashMap();
for(char ch:s.toCharArray()){
map.put(ch,map.getOrDefault(ch,0)+1);
}
ArrayListal=new ArrayList(map.values());
Collections.sort(al,Collections.reverseOrder());
int first=al.get(0);
int second =0;
for(int freq:al){
if(freq<first){
second=freq;
break;
}
}
for(Map.Entryentry:map.entrySet()){
if(second== entry.getValue()){
System.out.println(“The second most frequent character in the string is:”+entry.getKey());
break;
}
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
secondFrequent(s);
}
}
python code
from collections import Counter
def SecondMostFrequent(nums):
count=Counter(nums)
frequent=count.most_common(2)[1][0]
return frequent
arr=[1,1,1,1,1,1,5,5,5,2,6,6,8,8]
print(SecondMostFrequent(arr))
C++ solution class using MAP
class Solution {
public:
string s;
Solution(string str) : s(str) {}
char mostFreqChar() {
map cnt;
int maxi = INT_MIN;
char maxChar = ‘ ‘;
for (char c : s) {
cnt[c]++;
if (cnt[c] > maxi) {
maxi = cnt[c];
maxChar = c;
}
}
return maxChar;
}
};
#include
using namespace std;
bool cmp(pair&a, pair&b){
return a.second>b.second;
}
int main(){
string s=”abcdd”;
mapmp;
for(int i=0;i<s.length();i++){
mp[s[i]]++;
}
vector<pair>v;
for(auto it:mp){
v.push_back(it);
}
sort(v.begin(),v.end(),cmp);
int val=v[0].second;
for(int i=1;i<v.size();i++){
if(v[i].second<val){
cout<<v[i].first;
break;
}
}
}
a=input()
#making distinct data
lis=”
for i in a:
if i not in lis:
lis+=i
# print(lis)
#condition for all 1 values
# if lis==a:
# print(‘all have same 1’)
l=[]
for i in lis:
cou=a.count(i)
# print(cou,i)
l=l+[[i]+[cou]]
l.sort()
# print(‘sorted list is ‘ , l)
dicc= (dict(l))
# print(dicc)
val = list(dicc.values())
val.sort()
lval=val[-1]
# print(val)
#making condition of when we write max number 2
e_val=[]
for i in val:
if i not in e_val:
e_val+=[i]
if len(e_val)==1:
print(lis, ‘all are same times’)
# print(lval)
for j in range(lval):
out=”
for i in dicc:
if dicc[i]==j:
out+=str(i)
if out==”:
pass
else:
# print(val[-2])
if j==e_val[-2]:
print(out ,’–‘, j)
Anil#very simple python code
str=input()
l=list(str)
v=[]
for i in l:
v.append(l.count(i))
v=sorted(v)
v=[*set(v)]
for i in l:
if l.count(i)==v[-2]:
print(i)
break
#simple python code
n=input()
d={}
k=[]
L=[]
for i in n:
k.append(i)
for i in k:
if i not in d:
d[i]=1
else:
d[i]+=1
for i in d.values():
L.append(i)
L.sort()
A=L[-2]
if max(L)==1:
print(“No Second most frequent character”)
else:
for i,j in d.items():
if j==A:
print(i)
not all only 1
#if max(L)==1:
#print(“No Second most frequent character”)
if all elements in l are same but not 1
#include
#include
#include
using namespace std;
void checkPangram(string str){
vector freq(26,0);
int n=str.length();
int val=0;
transform(str.begin(),str.end(),str.begin(), ::tolower);
for(int i=0;i<n;i++){
freq[str[i]-97]+=1;
}
val=max_element(freq.begin(),freq.end())-freq.begin();
freq[val]=0;
// val=*max_element(freq.begin(),freq.end());
val=max_element(freq.begin(),freq.end())-freq.begin();
cout<<"Second most frequent: "<<char(val+97);
};
int main()
{
string str;
getline(cin, str);
checkPangram(str);
return 0;
}