Iterate over the array , arr [ ] and push the remaining occurrences of the elements into ans [ ]
for ( int i = 0 ; i < N ; i ++ ) { if ( i > 0 && arr [ i ] == arr [ i - 1 ] ) ans . push_back ( arr [ i ] ) ; }