Delete consecutive same words in a sequence | C ++ program to remove consecutive same words ; Function 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int removeConsecutiveSame ( vector < string > v ) { int n = v . size ( ) ; for ( int i = 0 ; i < n - 1 ; ) { if ( v [ i ] . compare ( v [ i + 1 ] ) == 0 ) { v . erase ( v . begin ( ) + i ) ; v . erase ( v . begin ( ) + i ) ; if ( i > 0 ) i -- ; n = n - 2 ; } else i ++ ; } return v . size ( ) ; } int main ( ) { vector < string > v = { " tom " , " jerry " , " jerry " , " tom " } ; cout << removeConsecutiveSame ( v ) ; return 0 ; }