- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find second most frequent character in C++
In this problem, we are given string str. Our task is to create a Program to find second most frequent character in C++.
Let’s take an example to understand the problem
Input
str = “abaacabcba”
Output
‘b’
Solution Approach
To find the character that is second most frequent in the string. We need to maintain a count array chatCount that is used to store the frequency of each character in the string. And then using the array we will find the character with max and secondMax frequency in the array. And display the second most frequent character.
Program to illustrate the working of our solution
Example
#include <iostream> #include <string.h> using namespace std; char findSecFreqChar(string str){ int charFreq[256] = {0}; for (int i = 0; i < str.length(); i++) (charFreq[str[i]])++; int maxFreq = charFreq[0], secFreq = charFreq[0]; for (int i = 0; i < 256; i++){ if (charFreq[i] > charFreq[maxFreq]){ secFreq = maxFreq; maxFreq = i; } else if (charFreq[i] > charFreq[secFreq] && charFreq[i] != charFreq[maxFreq]) secFreq = i; } return secFreq; } int main(){ string str = "tutorialspoint"; char secFreqChar = findSecFreqChar(str); cout << "Second most frequent character of the string is"<<secFreqChar; return 0; }
Output
Second most frequent character of the string is i
- Related Articles
- Find Second most frequent character in array - JavaScript
- Second most frequent character in a string - JavaScript
- Finding the second most frequent character in JavaScript
- Python program to find Most Frequent Character in a String
- C# program to find the most frequent element
- Find the second most frequent element in array JavaScript
- Returning the second most frequent character from a string (including spaces) - JavaScript
- Python program to find Least Frequent Character in a String
- Program to find frequency of the most frequent element in Python
- C++ program to find Second most repeated word in a sequence
- Most Frequent Subtree Sum in C++
- Program to find most frequent subtree sum of a binary tree in Python
- Write a program in C++ to find the most frequent element in a given array of integers
- Most Frequent Number in Intervals in C++
- Python program for most frequent word in Strings List

Advertisements