Returns count of words with frequency exactly 2.
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 ; }