Most frequent word in first String which is not present in second String | Function to return frequent word from S1 that isn 't  present in S2 ; create map of banned words ; find smallest and most frequent word ; check if word is not banned ; return answer ; Driver Code
function smallestFreq ( S1 , S2 ) { let banned = new Map ( ) ; for ( let i = 0 ; i < S2 . length ; i ++ ) { let s = " " ; while ( i < S2 . length && S2 [ i ] != ' ' ) s += S2 [ i ++ ] ; banned . set ( s , banned [ s ] == null ? 1 : banned . get ( s ) + 1 ) ; } let result = new Map ( ) ; let ans = " " ; let freq = 0 ; for ( let i = 0 ; i < S1 . length ; i ++ ) { let s = " " ; while ( i < S1 . length && S1 [ i ] != ' ' ) s += S1 [ i ++ ] ; if ( banned . get ( s ) == null ) { result . set ( s , result . get ( s ) == null ? 1 : result . get ( s ) + 1 ) ; if ( result . get ( s ) > freq || ( result . get ( s ) == freq && s < ( ans ) ) ) { ans = s ; freq = result . get ( s ) ; } } } return ans ; } let S1 = " " ; let S2 = " " ; document . write ( smallestFreq ( S1 , S2 ) ) ;