hi guys......
i have a problem in writing a program of "how to delete the duplicate elements in an array"
for example....if i enter an array like:
10 20 30 40 30
then it should print:
10 20 30 40

so could plz help me out????? :cry:
i have attempted little though....but don't know how can i go ahead....

#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[5],i,j;
clrscr();
printf("ENTER ARRAY ELEMENTS:");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
fflush(stdin);
for(i=0;i<5;i++)
{
for(j=i;j<4;j++)
{

//i don't know now where do i go....from here on.....how to find the element which is repeating that is the duplicate and then how to delete that.....

so plz help me out......

Recommended Answers

All 49 Replies

could any 1 reply plz............

>could any 1 reply plz............
I was going to help you, until I saw that you bumped your thread before 30 minutes had passed. That's incredibly rude.

Try this: [Useful suggestion removed because you don't deserve it. Figure the solution out on your own]

im really sorry but im new to this site so don't know how exactly it works...but anyways.....i don't know how to go from here on.....i did'nt understand what exactly were u trying to say......

i have been online here for 2 hrs..almost and didn't get any reply from any1/../so i was checking on some other threads of this site....as im new to this site...so plz don't take it otherwise...and help me out.......plz with this problem......

So you want to see if any of a[]'s values duplicate any OTHER of a[]'s values?

You've got a start there with the two loops, so how about something like:

for each element (that's a in the first loop, say)
look through every OTHER element in the array to see if it matches; in your code fragment that would be the 'for j' loop. Something like (and I am paraphrasing, not writing compilable code here):

for i 0..5
for j 0..5
if i is not j, then if a equals a[j] you have a match between the elements at i and j!

The next part is to figure out how to remove that duplicate. One simple approach is to copy the non-dups down over the dup (so if 3 is the dup, set [3] = [4] and [4] = [5]). Another approach is to have a separate array of non-duplicate values, like this:

(from the above loops, just below the close of the for j loop)

if not a duplicate, then copy a over to nonDuplicates[countOfNonDuplicates] and increment the countOfNonDuplicates.

hi guys......
i have a problem in writing a program of "how to delete the duplicate elements in an array"
for example....if i enter an array like:
10 20 30 40 30
then it should print:
10 20 30 40

so could plz help me out?????
i have attempted little though....but don't know how can i go ahead....

#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[5],i,j;
clrscr();
printf("ENTER ARRAY ELEMENTS:");
for(i=0;i<5;i++)
scanf("%d",&a);
fflush(stdin);
for(i=0;i<5;i++)
{
for(j=i;j<4;j++)
{

//i don't know now where do i go....from here on.....how to find the element which is repeating that is the duplicate and then how to delete that.....
so plz help me out......

for finding out the duplicates here...first...i have done like :
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a==a[j])
printf("duplicate element exists:");
else
printf("duplicate elements does not exist:");
}
}
but when i run this it doesnot run properly and gives some weird output....so what do i do now?

help me out guys plzzz!!!!!!! im waiting for ur reply....any kind of help would be highly appreciable.......

You don't say what kind of 'weird output' you get, but I imagine it always finds duplicates, because there will always be an a == a[j] unless you say "if ((i !=j) && (a == a[j]))"

By the way, I tend to look at this site once or twice a day, and I imagine others do as well, so waiting 36 minutes for a reply and then being impatient with us is expecting a lot. Remember, we do this for fun and not for money!

i have been online here for 2 hrs..almost and didn't get any reply from any1/../so i was checking on some other threads of this site....as im new to this site...so plz don't take it otherwise...and help me out.......plz with this problem......

2 hours?!?!? You've not gotten an answer in 2 hours, and you're complaining?!?!?

Come on now. We're doing this for free-- you have no right to complain even if we don't answer in 2 days! Please be more mindful in the future. If nobody answers your question promptly, it's likely because we don't have the answer, or the right person has not yet visited the site.

Please be more considerate of other forum members, and don't bump your post in the same day you posted.

>it's likely because we don't have the answer, or the right person has not yet visited the site.
Or the first person who wanted to help was too insulted and deleted the meat of her post. :D Anyway, it's best to just ignore people like galmca. They go away after a short time and leave us in peace.

Well, galmca has legitemate questions and supplies some sample code, so s/he just needs to learn a little etiquate. That's better than the people who type in the question the professor posed and wants us to do their homework.

#include <stdio.h>
 int main(){int*i,*j,a[]={10,20,30,40,30};for(i=a;i<&a[sizeof a/sizeof*a];++i){for(j=a;j<i;++j){if(*i==*j){break;}}if(i==j){printf(&",%d"[i==a],*i);}}putchar('\n');return 0;}

Well, galmca has legitemate questions and supplies some sample code, so s/he just needs to learn a little etiquate. That's better than the people who type in the question the professor posed and wants us to do their homework.

Very true, but I think according to Narue, there's a trend possibly forming...

#include <stdio.h>
 int main(){int*i,*j,a[]={10,20,30,40,30};for(i=a;i<&a[sizeof a/sizeof*a];++i){for(j=a;j<i;++j){if(*i==*j){break;}}if(i==j){printf(&",%d"[i==a],*i);}}putchar('\n');return 0;}

What, no recursion?

#include<stdio.h>
#include<stdlib.h>
a[20]={0};i=0;n=20;d(j)int(j);{j<n?printf("%-3i",
j++[a]),d(j):putchar('\n');}e(j)int(j);{j<20?j++[
a]=rand()%10,e(j):d(0);}f(j){j<n-1?j[a]=a[j+1],f(
j+1):--n;}g(i,v)int(i),v;{return(i>=n?i:i[a]==v?f
(i),g(i,v),i:g(i+1,v),i);}main(){!i?e(0):i==n?d(0
):0;return(i!=n?(i=g(i+1,i[a])),main():0);}

:mrgreen:

im really sorry but i didn't get the code which has been provided to me...it seems really complex when i read that....so could u plz help me out in the terms of the way i wrote....that way i'll be able to understand better...like to find out the duplicate element in an array...
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a==a[j])
printf("duplicate exists");
else
printf("duplicate does not exists");
}
}

is that way wrong?

I think on my part, Narue is a smart person, but does have a short fuse for newbies, i was new, but i found fast how to fit in better, read some other post's, to figure out how things go, some people arn't like me and go to the extent to fit in. galmca does have a good question, since ima newbie, and just learned the loops and what not, idont know how to helpm, Narue, you'r smart, but atleast don't be so rude about it, and yes galmca, i have 2 post'sd that have been here for days and, no answer, so i took matters into my own hands, and did it myself. They arn't paid, so sometimes, do it yourself has to be sufficient..

That code there sounds about correct to me galmca, of course from above post, im new, and don;t know, but i just finished reading that section ;P

>is that way wrong?
You're on the right track, but it will give false positives. Compare this:

#include <stdio.h>

int main ( void )
{
  int a[7] = {1,2,3,2,4,1,0};
  int i, j;

  for ( i = 0; i < 7; i++ ) {
    for ( j = 0; j < 7; j++ ) {
      if ( a[i] == a[j] )
        printf ( "Duplicate found: %d == %d\n", i, j );
    }
  }

  return 0;
}

With almost identical code that has an added test for self-comparison. If a and a[j] are not the same element but have the same value, a duplicate was found:

#include <stdio.h>

int main ( void )
{
  int a[7] = {1,2,3,2,4,1,0};
  int i, j;

  for ( i = 0; i < 7; i++ ) {
    for ( j = 0; j < 7; j++ ) {
      if ( &a[i] != &a[j] && a[i] == a[j] )
        printf ( "Duplicate found: %d == %d\n", i, j );
    }
  }

  return 0;
}

You'll also notice that this finds each duplicate twice, once for a == b and once for b == a. You can fix the problem by starting the inner loop at i instead of 0 because everything up to i has already been tested:

#include <stdio.h>

int main ( void )
{
  int a[7] = {1,2,3,2,4,1,0};
  int i, j;

  for ( i = 0; i < 7; i++ ) {
    for ( j = i; j < 7; j++ ) {
      if ( &a[i] != &a[j] && a[i] == a[j] )
        printf ( "Duplicate found: %d == %d\n", i, j );
    }
  }

  return 0;
}

Oh and for your future info, galmca when you post a code type CODE in [] (

)
and at the end of the code put /CODE in [] (

)

oops put CODE inside of [] and put /CODE inside of [] at the end of it

i have tried using that what u just told.....but it is still giving very weird output when i enter 5 array elements: 1 2 3 1 4 ....then it should print duplicate exists:1
instead of printing that it is printing 10 or 15 times...duplicate does not exists:
now what do i do now??

>now what do i do now??
If you have a problem with code you've written, describe the problem and POST THE CODE!

>i have tried using that what u just told
I'm guessing that you took the code I posted and butchered it into something broken.

could any 1 tell plz...what do i do now?
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if((i!=j)&&(a==a[j]))
printf("duplicate exists");
else
printf("duplicate does not exists");
}
}
it is still giving very weird out put by printing 10 or 15 times duplicate does not exists when i run the program...otherwise there is no any complilation error....
when i run this code and i give...5 values of an array like...
1
2
3
1
4
then instead of printing:
duplicate exists:1
it gives weird out put like i mentioned above.......so plz help me out.........i would be very greatful if u can.......

>it gives weird out put like i mentioned above
It's not weird, it's exactly what you asked for. If there's a duplicate, print "duplicate exists" and every time there is not a duplicate, print "duplicate does not exists". Change your code to this to get what you want:

#include <stdio.h>

int main ( void )
{
  int a[] = {1,2,3,1,4};
  int i, j;

  for(i=0;i<5;i++)
  {
    for(j=i;j<5;j++)
    {
      if((i!=j)&&(a[i]==a[j]))
        printf("duplicate exists: %d\n", a[j]);
    }
  }

  return 0;
}

try out this code

#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[5],i,j;
clrscr();
int len=5;
printf("ENTER ARRAY ELEMENTS:");
for(i=0;i<5;i++)
scanf("%d",&a);
fflush(stdin);
for(i=0;i<5;i++)
{
for(j=i;j<4;j++)
{
if(a==a[j])
{
a[j]=a[j+1];
len--;
j--;
}
}
}
for(i=0;i<len;i++)
printf("%d\n",a);
}

>try out this code
Why bother? It won't work, and even if it did I wouldn't run it anyway because it has undefined behavior. Shall we tick off the problem areas?

>#include<conio.h>
This program does nothing that warrnats features from conio.h. You should avoid this nonstandard and highly nonportable header whenever possible.

>int main(void)
At least you got this right.

>clrscr();
Very antisocial. If you're running the program in an IDE window then there's no reason to clear the screen. If you're running the program on a command line then clearing the screen will piss off anyone who was interested in the output of previously run programs. Either way you don't want to clear the screen.

>int len=5;
This is why the program won't compile, which tells me that you're trying to write C programs and compile them as C++. This leads to all sorts of bad style and error prone C code. Remember: In C, declarations must be at the beginning of a block. Because you called clrscr(), the declaration for len is not at the beginning of the block. [Note: In C99 you can declare variables anywhere, but you're not probably not using C99 as I don't know of a compiler that supports C99 and conio.h]

>printf("ENTER ARRAY ELEMENTS:");
You didn't flush the output stream, so some users may not get this prompt. The program will sit and wait for user input and the user will sit and wait for a prompt for input. This is called a deadlock, and it is to be avoided.

>scanf("%d",&a);
Your lack of error checking is disappointing. User input should always be tested for validity, and any use of scanf should be tested carefully because nobody seems to know how to use it correctly.

>fflush(stdin);
This is undefined, fflush is only defined to work on output streams.

><snip loop>
Did you even compile and run this code? It will loop infinitely. What's even more annoying is that the reason it will loop infinitely is because you made a mistake that I pointed out very clearly in this thread as a potential problem. Though if it makes you feel any better, once the self-comparison problem is fixed, the loop will still be broken for just about every input sequence.

>printf("%d\n",a);
>}
You declare main correctly and then forget to return a value. This is undefined behavior unless you're compiling as C++ or C99. Since we've already established that you aren't compiling as C99, I have to again assume C++, which is still a bad idea.

Shall we look at the same program with the changes needed to make it work?

#include<stdio.h>

int main ( void )
{
  int a[5], len;
  int i, j, k;

  printf ( "ENTER ARRAY ELEMENTS: " );
  fflush ( stdout );
  for ( len = 0; len < 5; len++ ) {
    if ( scanf ( "%d", &a[len] ) != 1 )
      break;
  }

  for ( i = 0; i < len; i++ ) {
    for ( j = i; j < len; ) {
      if ( i != j && a[i] == a[j] ) {
        for ( k = j; k < len - 1; k++ )
          a[k] = a[k + 1];

        --len;
      }
      else
        ++j;
    }
  }

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

  return 0;
}

thx alot narue..... my code is running fine now......but im also really very sorry about....that day when i complained about not getting my program done .....to be honest with u this is the first time i have ever been into any forum site where ican actually put up questions and try to solve a problem as im a newbie here.....so i really didn't know at all about the rules about this site and how do the people work here so im really sorry about that......now i know that u people do thsi just for the convienience of us for free....so that's really nice to know.....and im proud of u guys!!!!!!!
just don't mind plz.......im not a girl like what u thought........at all......im really sorry once again......if i made u feel like that.......plz forgive me......if u can.......i'll be very greatful to u.......and thx alot for ur help and that u took time out for me..........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.