Delete consecutive same words in a sequence | C # implementation of above method ; Method to find the size of manipulated sequence ; Start traversing the sequence ; Push the current string if the stack is empty ; compare the current string with stack top if equal , pop the top ; Otherwise push the current string ; Return stack size ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int removeConsecutiveSame ( List < string > v ) { Stack < string > st = new Stack < string > ( ) ; for ( int i = 0 ; i < v . Count ; i ++ ) { if ( st . Count == 0 ) { st . Push ( v [ i ] ) ; } else { string str = st . Peek ( ) ; if ( str . Equals ( v [ i ] ) ) { st . Pop ( ) ; } else { st . Push ( v [ i ] ) ; } } } return st . Count ; } public static void Main ( string [ ] args ) { List < string > v = new List < string > ( ) ; v . Add ( " ab " ) ; v . Add ( " aa " ) ; v . Add ( " aa " ) ; v . Add ( " bcd " ) ; v . Add ( " ab " ) ; Console . WriteLine ( removeConsecutiveSame ( v ) ) ; } }