removing duplicate elements in an array

hey.i have been trying to remove duplicate elements from an array of 6 elements.
eg if input is
1 2 2 3 4 5
the output will be 1 2 3 4 5
the program i tried for is:

#include<stdio.h>

main()
{
    int a[120];int i;
    int c;
    char b=' ';
    for(i=0;i<6;i++)
    {
        scanf("%d",&a[i]);
    }

    for(i=0;i<6;i++)
    {

        for(c=0;c<6;c++)
        {
            ++c;
            if(a[i]==a[c])
            printf("%c",b);
          

        }

        printf("%d",a[i]);
    }
}

i cant rectify the error::

Is array ordered?

It seems, that you are beginning with programming, first try to find some description (in words/sentences/pseudo code) how you want to get your goal and if that works fine, then try to implement it.

I know how to solve it, but your code seems incorrect to me (f.e. you print b, but value of b is always ’ ', you increment loop variable c, typically you don’t want to do that)

hey i tried the same with java!

import java.io.*;
class duplicate
{
int n;int count;
void input()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“enter the length”);
n=Integer.parseInt(br.readLine());
int ar[]=new int[n]; int m=0;
for(int i=0;i<n;i++)
{
ar[i]=Integer.parseInt(br.readLine());
}
System.out.print(“The array after removing duplicate elements is---->”);
System.out.print(ar[0]+" “);
for(int j=0,count=0;j<n&&count<n;count++ )
{
if(count==n-1)
{
if(ar[j]==ar[m])
{
while(ar[j]==ar[m])
{
m++;
if(m==n)
{
System.exit(0);
}
}
j=m;
System.out.print(ar[m]+” ");
count=0;
}
else
{
j++;
count=0;
}
}
}
}
public static void main(String args[])throws IOException
{
duplicate ob=new duplicate();
ob.input();
}
}

And this is question or answer? I didn’t get it…

What about this code?

#include<stdio.h>

int main()
{

int a[120],i,c,f,n;
scanf("%d",&n);
for(i=0;i<n;i++)
	scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
    for(c=i+1;c<n;c++)
	{
    	if(a[i]==a[c])
    		break;
    	if(c==n-1)
    		printf("%d ",a[i]);
    }
}
printf("%d\n",a[n-1]);
return 0;

}

the third inner for loop is weird…it doesn’t really serve a purpose

#include<stdio.h>

main()
{

int a[120];int i;
int c;
char b=' ';
for(i=0;i<6;i++)
{
    scanf("%d",&a[i]);
}

int num=6, k;
for(i=0;i<num;i++)
{

    for(c=(i+1);c<num;c++)
    {
        
        if(a[i]==a[c]){
           for(k=c;k<num;k++)
            a[k]=a[k+1];
           num--;
          c--;
           }
    }

      }

for(i=0;i<num;i++)
printf("%d ", a[i]);

}

#include<stdio.h>
#include<set.h>
int main() {

   int A[100000],n;
   scanf("%d",&n);

   for(int i=0;i<n;i++)
      scanf("%d",&A[i]);

   set<int> S(A,A+n);

   for(set<int>::iterator it=S.begin();it!=S.end();it++)
      printf("%d ",*it);
   printf("\n");

   return 0;
   }

Even though lots of people posted code here, I believe that the best approach to help on such a problem is to actually explain what data structure needs to be used and also what’s the underlying idea if you don’t know such data structure.

The basic idea here, is that you have a certain list, let’s call it S’ such that it contains several duplicate elements.

So, for instance:

S’ = [1,4,5,6,2,4,3,2,1]

The basic idea to solve your problem easily could be to create an auxiliar array, that will hold only the distinct elements that are on the original set. Let this list be called Aux. So:

Aux = [1,4,5,6,2,3]

This auxiliar data structure can be constructed in the following manner:

  1. Initially, it is empty:
    Aux = []

  2. Append 1st element of S’ to it:
    Aux = [1]

  3. Check if the 2nd element of S’ is equal to the 1st one already in Aux:
    3.1 If it is, advance to next element, leaving Aux unchanged;
    3.2 If it is not, append it to Aux, and advance to next element;

  4. Repeat procedure 3 until you’ve reached the end of S’.

The complexity of such approach is O(N^2), but we can do much better than this…

It turns out, that a data structure exists, called a Set, which is, completely similar to its mathematical twin, especially in the sense that it can’t contain any duplicate elements and the elements on a given set are always ordered… More info on sets, [here][1].

It is relatively easy to implement such a data structure since it is built-in on the vast majority of programming languages :slight_smile:

As a reference, here is a Python code, that removes all duplicate elements from a list:

L = [1,3,3,5,6,7,1,5,4,4,16,8,6]
print list(set(L))

The above code will return the list: [1,3,4,5,6,7,8,16]

Note that sets are immutable, so you can’t change a set directly, that’s the reason I converted it back into a list on my code… Hope I could help you out :wink:
[1]: http://en.wikipedia.org/wiki/Set_(mathematics)

1 Like

What if L was: [1,3,5,6,7,3]??

It would be [1,3,5,6,7]