
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find Second most repeated word in a sequence
We are given an array of words, and we need to find the word whose frequency is the second largest in the array.
Let’s assume some simple input and output scenarios
Let’s assume we are having an array which consists of elements like [“point,” “world,” “articles,” “articles,” “articles,” “world,” “world,” “world,” “point”].
The frequency of words are −
“point”: 2 “world”: 4 “articles”: 3 // This will be the second most repeated word in the array.
So the second most repeated word is “articles,” and our output is “articles.”
Let’s consider another scenario, Where there are group of similar words in an array, for example [“abc”, “abc”, “abc”, “bab”, “bab”, “cat”]. The second most repeated word in sequence will be “bab”.
“abc” = 3 “bab” = 2 // This will be the second most repeated word in the array. “cat” = 1
We can hash and find the frequencies of each word in the given array of words and then return the second greater. In C++, we can use the unordered_map data structure.
Algorithm
The following are steps to be followed to perform the task
Implement a hash table.
The frequency of each string in the vector should be stored in hash table.
Traverse the hash table after storing each string in hash table to find the string having second highest frequency.
Then, print the string as output
Example
The C++ implementation to find the second most frequent word in a list is as follows −
#include <iostream> #include <vector> #include <unordered_map> using namespace std; string solve(vector<string> words) { unordered_map<string, int> hash; for (auto word: words) { hash[word]++; } int first_max = INT32_MIN, sec_max = INT32_MIN; for (auto it: hash) { if (it.second > first_max) { sec_max = first_max; first_max = it.second; } else if (it.second > sec_max && it.second != first_max) { sec_max = it.second; } } for (auto it: hash) { if (it.second == sec_max) { return it.first; } } return ""; } int main() { vector<string> words = { "point", "world", "articles", "articles", "articles", "world", "world", "world", "point" }; cout << solve(words); return 0; }
Output
“articles”
Conclusion
We can also use maps in C++, but it is useless as we don’t want our words to be sorted lexicographically when hashed. Using the map will also be expensive as the insert operation takes O(log(n)) while in unordered_map, it takes O(1). For bigger input, though, we have to write our custom hash function to avoid conflicts and keep the time complexity O(1) only.
- Related Articles
- Find the second most repeated word in a sequence in Java
- Second most repeated word in a sequence in Python?
- How to find Second most repeated string in a sequence in android?
- How to Find the Most Repeated Word in a Text File using Python?
- Program to find second most frequent character in C++
- Find the first repeated word in a string in C++
- Write a program in Python to find the most repeated element in a series
- Program to find length longest prefix sequence of a word array in Python
- Find the first repeated word in a string in Python?
- Find the first repeated word in a string in Java
- Which is your second most favorite 'F' word?
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- C++ Program to Find kth Largest Element in a Sequence
- Find the first repeated word in a string in Python using Dictionary
- Write a program in Python to print the most frequently repeated element in a series
