Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
The code i am implementing is supposed to remove the duplicates in an sorted array .But it does not work properly
Here is the code
The output should be 1,3,4 but instead the output coming is 1 1 3 3 4
Help needed :)

#include<stdio.h>

void remove_dup(int n ,int a[])
{
  int i=0,j=0;
  while(i<=n){
      if(a[i]==a[i+1])
	{
	  i=i+1;
	}
   else if(a[i]!=a[i+1])
	{
	  a[j]=a[i];
	  j++;
	  i++;
	  printf("%d\n",a[j]);	
}
    }
}
 int main()
  {
    int a[]={1,1,1,3,3,4,5};
    remove_dup(7,a);
  
}
Posted
Comments
nv3 22-Sep-13 3:31am    
How should remove_dup be able to remove anything from the array? It cannot change the first argument n, as it is transferred by value! So it cannot shorten the array when it finds duplicates.

Inside you remove_dup the while-loop runs to far, should be i < n. And the if - else conditions don't make any sense. j is declared but never used, except in the printf statement.

1 solution

First off, you don't need to check if it is different twice:
C#
if(a[i]==a[i+1])
   ...
else if(a[i]!=a[i+1])
   ...
Is the same as:
C#
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:
C++
#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]
 
Share this answer
 
v2
Comments
ridoy 22-Sep-13 6:36am    
5ed.
H.Brydon 22-Sep-13 23:08pm    
Changed from providing advice to doing homework now?
OriginalGriff 23-Sep-13 2:30am    
:laugh:
Boring weekend, plus his version worked, it just needed a couple of tweaks he hadn't thought of and some 'stylistic' work.
Difficult to explain the style and readability things without actually doing them, so I did!
H.Brydon 23-Sep-13 10:50am    
Gone to the dark side you have.
OriginalGriff 23-Sep-13 11:17am    
OriginalSith! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900