Find one extra character in a string | Javascript program to find extra character in one string ; Determine String with extra character . ; Add character codes of both the strings ; Add last character code of large String . ; Minus the character code of smaller string from the character code of large string . The result will be the extra character code . ; Driver code
function findExtraCharacter ( s1 , s2 ) { let smallStr ; let largeStr ; if ( s1 . length > s2 . length ) { smallStr = s2 ; largeStr = s1 ; } else { smallStr = s1 ; largeStr = s2 ; } let smallStrCodeTotal = 0 ; let largeStrCodeTotal = 0 ; let i = 0 ; for ( ; i < smallStr . length ; i ++ ) { smallStrCodeTotal += smallStr [ i ] . charCodeAt ( 0 ) ; largeStrCodeTotal += largeStr [ i ] . charCodeAt ( 0 ) ; } largeStrCodeTotal += largeStr [ i ] . charCodeAt ( 0 ) ; let intChar = largeStrCodeTotal - smallStrCodeTotal ; return String . fromCharCode ( intChar ) ; } let s1 = " " ; let s2 = " " ; let extraChar = findExtraCharacter ( s1 , s2 ) ; document . write ( " " + extraChar ) ;