I want to create a function that outputs the distinct values in a given array. For example, in the following sequence 2 2 1 1 5 2 , the distinct digits are 2 1 5. What I've done till now is:
void Distinct(int a[], int b[], int n){
int i; //n is the number of elements for a and b
int k=0; //a is the original array
int ac;
b[0] = a[0];
for (i = 0; i <= n; ++i){
if (a[i] != b[k])
{
k+=1;
ac = a[i];
b[k] =ac; //b is where I want to put distinct digits
}
}
for ( i = 0; i < k; ++i)
{
printf("%d ", b[i]);
}
}
But this is not enough, because what I've done is for the case when the numbers repeat one after another, I mean: 111122221 , I will get 121, and 1 at the end again.Can you please help me write a function that would consider this case, with repeating numbers scattered in the array ? Thank you.