Count words that appear exactly two times in an array of words | Returns count of words with frequency exactly 2. ; Driver code
function countWords ( str , n ) { var m = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( m . has ( str [ i ] ) ) m . set ( str [ i ] , m . get ( str [ i ] ) + 1 ) else m . set ( str [ i ] , 1 ) } var res = 0 ; m . forEach ( ( value , key ) => { if ( ( value == 2 ) ) res ++ ; } ) ; return res ; } var s = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; var n = s . length ; document . write ( countWords ( s , n ) ) ;