First of all your program has undefined behaviour because it tries to access memory beyond the array in the loop when i is equal to 8.
for(int i = 0; i < 9; i++) {
if(arr[i] == arr[i + 1]) {
^^^^^^
The element with index equal to 9 (arr[8 + 1]) does not exist.
Moreover the logic is wrong. You are incrementing i twice when arr[i] is equal to arr[i+1]
for(int i = 0; i < 9; i++) {
^^^^
if(arr[i] == arr[i + 1]) {
newarr[counter] = arr[i];
i += 1;
^^^^^^
counter++;
} else {
but the next element with index arr[i+2] also can be equal to arr[i]. So the same value will be written at least twice in the destination array.
Try for example to apply your program to an array like this
int arr[] = { 1, 1, 1, 1, 1, 1 };
So you need to rewrite your program entirely.:)
As for the recursive function then it can look the following way
#include <stdio.h>
int * unique( const int *a, size_t n, int *b )
{
if ( n == 0 ) return b;
if ( n == 1 || a[0] != a[1] ) *b++ = *a;
return unique( a + 1, n - 1, b );
}
int main( void )
{
int a[] = { 1, 1, 5, 4, 7, 7, 9, 9, 8 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
printf( "\n" );
int b[N];
int *last = unique( a, N, b );
for ( int *first = b; first != last; ++first ) printf( "%d ", *first );
printf( "\n" );
}
Its output is
1 1 5 4 7 7 9 9 8
1 5 4 7 9 8
If you compiler does not support the C99 STandard then the program can look like
#include <stdio.h>
int * unique( const int *a, size_t n, int *b )
{
if ( n == 0 ) return b;
if ( n == 1 || a[0] != a[1] ) *b++ = *a;
return unique( a + 1, n - 1, b );
}
#define N 9
int main( void )
{
int a[N] = { 1, 1, 5, 4, 7, 7, 9, 9, 8 };
int b[N];
size_t i;
int *first, *last;
for ( i = 0; i < N; i++ ) printf( "%d ", a[i] );
printf( "\n" );
last = unique( a, N, b );
for ( first = b; first != last; ++first ) printf( "%d ", *first );
printf( "\n" );
}