-2

I've tried to write a program that removes the duplicate values from an array. I've partly managed to do so since my program is able to remove any ONE of the numbers which are repeated TWICE in the array. So the problem is that if a number is repeated thrice only one of the number is removed, i.e. the other two is still left in the array, also if more than one number is repeated even then only the number which comes first in the array is removed. I really cannot understand what's wrong with my code and why is it unable to remove numbers that are repeated more than two times. I've already surfed through the internet regarding this issue and though I got different ways to remove the duplicate elements, I still don't know what's wrong with my code.

#include <stdio.h>
#include <stdlib.h>

int dup(int [],int);
int main() 
{
    int i,n,index,a[20];
    printf("Enter n value \n");
    scanf("%d",&n);
    printf("Enter array values \n");
    for(i=0;i<n;++i)
        scanf("%d",&a[i]);
    for(i=0;i<n;++i)
    {
        index=dup(a,n);
        if(index==-1)
        {
            printf("No duplicate elements");
            break;
        }
        else
        {
            a[index]=0;
            for(i=index;i<n;i++)
                a[i]=a[i+1];
            n-=1;
        }
    }
    printf("Output: \n");
    for(i=0;i<n;++i)
        printf("%d\n",a[i]);

    return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
    int i,j,pos=-1;
    for(i=0;i<size;i++)
    {
        for(j=i+1;j<size;j++)
        {
            if(a[i]==a[j])
            {
                pos=j;
                return pos;
            }
        }
    }
    if(pos==-1)
        return pos;
}

OUTPUT

Enter n value

5

Enter array values

12

24

3

12

24

Output:

12

24

3

24

It clearly fails to remove the other repeated element "24". Also if a number was repeated thrice only one of the number would be removed.

Vishist Varugeese
  • 1,500
  • 1
  • 17
  • 30
  • Possible duplicate of [Algorithm: efficient way to remove duplicate integers from an array](http://stackoverflow.com/questions/1532819/algorithm-efficient-way-to-remove-duplicate-integers-from-an-array) – D4ttatraya Jan 25 '17 at 07:54

4 Answers4

2
for(i=0;i<n;++i)  // <-------------------------------------- for i
{
    index=dup(a,n);
    if(index==-1)
    {
        printf("No duplicate elements");
        break;
    }
    else
    {
        a[index]=0;
        for(i=index;i<n;i++) // <--------------------------- for i
            a[i]=a[i+1];
        n-=1;
    }
}

You are using the same loop variable for two loops, one nested inside the other. This cannot work. Use different variables. Live demo.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

My mistake, at first glence I thought you had problem with n after running this it worked.

#include <stdio.h>
#include <stdlib.h>

int dup(int [],int);
int main()
{
    int i,n,index,a[20], count;
    printf("Enter n value \n");
    scanf("%d",&n);
    count = n;
    int j;
    printf("Enter array values \n");
    for(i=0;i<n;++i)
        scanf("%d",&a[i]);
    for(i=0;i<n;++i)
    {
        index=dup(a,n);
        if(index==-1)
        {
            printf("No duplicate elements");
            break;
        }
        else
        {
            a[index]=0;
            for(j=index;j<n;j++)
                a[j]=a[j+1];
            n-=1;
        }
    }
    printf("Output: \n");
    for(i=0;i<n;++i)
        printf("%d\n",a[i]);

    return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
    int i,j,pos=-1;
    for(i=0;i<size;i++)
    {
        for(j=i+1;j<size;j++)
        {
            if(a[i]==a[j])
            {
                pos=j;
                return pos;
            }
        }
    }
    if(pos==-1)
        return pos;
}

OUTPUT

Enter n value

5

Enter array values

12

24

3

12

24

Output:

12

24

3

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
0

The Problem seem to lie in the if condition in second loop.

for (k = j; k < size; k++) {
           arr[k] = arr[k + 1];
        }

Simply put this piece of code after your if condition

if(a[i]==a[j])

and it will work.

Seeker
  • 303
  • 7
  • 17
0

You should name your iterator variables better so you might not confuse them in nested loops, or as you do, use the same twice in a nested loop. This skips all variables after your first removal.

and you don't have to do this

if(pos==-1)
    return pos;

skip the if as it is not necessary and if at this position posis not -1then you would have no return which would be UB I think.

Kami Kaze
  • 2,069
  • 15
  • 27