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 ] ) { j += 1 ; ans [ j ] = arr [ i ] ; } }