Count words that appear exactly two times in an array of words | C ++ program to count all words with count exactly 2. ; Returns count of words with frequency exactly 2. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWords ( string str [ ] , int n ) { unordered_map < string , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ str [ i ] ] += 1 ; int res = 0 ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; it ++ ) if ( ( it -> second == 2 ) ) res ++ ; return res ; } int main ( ) { string s [ ] = { " hate " , " love " , " peace " , " love " , " peace " , " hate " , " love " , " peace " , " love " , " peace " } ; int n = sizeof ( s ) / sizeof ( s [ 0 ] ) ; cout << countWords ( s , n ) ; return 0 ; }