Remove all duplicate adjacent characters from a string using Stack | Function to remove adjacent duplicate elements ; Store the string without duplicate elements ; Store the index of str ; Traverse the string str ; Checks if stack is empty or top of the stack is not equal to current character ; If top element of the stack is equal to the current character ; If stack is empty ; If stack is not Empty ; Driver Code
function ShortenString ( str1 ) { var st = [ ] ; var i = 0 ; while ( i < str1 . length ) { if ( st . length == 0 str1 [ i ] != st [ st . length - 1 ] ) { st . push ( str1 [ i ] ) ; i ++ ; } else { st . pop ( ) ; i ++ ; } } if ( st . length == 0 ) { return ( " " ) ; } else { var short_string = " " ; while ( st . length != 0 ) { short_string = st [ st . length - 1 ] + short_string ; st . pop ( ) ; } return ( short_string ) ; } } var str1 = " " ; document . write ( ShortenString ( str1 ) ) ;