First off, you don't need to check if it is different twice:
if(a[i]==a[i+1])
...
else if(a[i]!=a[i+1])
...
Is the same as:
if(a[i]==a[i+1])
...
else
...
Second, the values you are expecting are wrong: it's not 1, 3, 4 you should get, but 3, 4, 5 - those are the ones you need to move...
Third, you need to return the new length, or the outside world can't tell how many unique elements there were - and so how much data the outside world should process now.
Fourth, you need to check your values - your while loop will run off the end of the array! :laugh:
Fifth, it's also a good idea to use longer, more meaningful names - it helps to self dcoument the code when you read it.
Sixth, get your indentation right, and
be consistent in your curly bracket positioning! Having the style change makes it much, much harder to read...
So, try this:
#include <stdio.h>
int remove_dup(int count ,int arr[])
{
int in=0,out=0;
while(in<count)
{
if(arr[in]!=arr[out])
{
out++;
arr[out]=arr[in];
}
in++;
}
return out + 1;
}
int main(void)
{
int index;
int data[]={1,1,1,3,3,4,5};
int newLen = remove_dup(7,data);
for (index = 0; index < newLen; index++)
{
printf("%d,", data[index]);
}
printf("\nPress ENTER to close. ");
getchar();
return 0;
}
[edit]closing bold in wrong place :O - OriginalGriff[/edit]