Delete consecutive same words in a sequence | C # program to remove consecutive same words ; Method to find the size of manipulated sequence ; Start traversing the sequence ; Compare the current string with next one Erase both if equal ; Erase function delete the element and also shifts other element that 's why   i is not updated  ; Update i , as to check from previous element again ; Reduce sequence size ; Increment i , if not equal ; Return modified size ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int removeConsecutiveSame ( List < string > v ) { int n = v . Count ; for ( int i = 0 ; i < n - 1 ; ) { if ( v [ i ] . Equals ( v [ i + 1 ] ) ) { v . RemoveAt ( i ) ; v . RemoveAt ( i ) ; if ( i > 0 ) { i -- ; } n = n - 2 ; } else { i ++ ; } } return v . Count ; } public static void Main ( string [ ] args ) { List < string > v = new List < string > ( ) ; v . Add ( " tom " ) ; v . Add ( " jerry " ) ; v . Add ( " jerry " ) ; v . Add ( " tom " ) ; Console . WriteLine ( removeConsecutiveSame ( v ) ) ; } }