helos...!!!
wel am new to dis..!!!
but i hav a code to it...!!!
lets try out..!!!!!

#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j,count=1,flag,arr[5],p[5];

  for(i=0;i<5;i++)
    scanf("%d",&arr[i]);

  p[0]=arr[0];
  for(i=1;i<5;i++)
  {
    flag=0;
    for(j=0;j<=count;j++)
    {
      if(arr[i]==p[j])
        flag=1;
    }

    if(flag==0)
    {

      p[count]=arr[i];
      count++;
    }
  }
  for(i=0;i<count;i++)
  {
    printf("%d\t",p[i]);
  }
  getch();
}

try dis out dude..!!!

Recommended Answers

All 2 Replies

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);
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......

hi,

you take element by elment from the array and compare with all the element, if equal just remove. proceed otherwise.
just do as we are sorting.

bye

>helos...!!!
>wel am new to dis..!!!
>but i hav a code to it...!!!
>lets try out..!!!!!
You have one of the most annoying writing styles I've seen. If you want people to treat you like anything but an irritating script kiddie, use proper English. The non-native English speakers will thank you.

>try dis out dude..!!!
1) Your code uses a poor style.
2) Your code doesn't work.

Please test solutions before you post them, unless you want someone like me to rip them to shreds, like I'm about to. Let's start with the extremely poor style you're using (I even fixed the indentation for you when I added code tags):

>#include<conio.h>
This is a non-portable header. You'd do well to forget it exists until you're experienced enough to use it wisely, which you most certainly are not right now.

>void main()
There are two standard definitions of main:

int main ( void )
{
  return [I]<integer value>[/I];
}

and

int main ( int argc, char *argv[] )
{
  return [I]<integer value>[/I];
}

The portable return values are 0, EXIT_SUCCESS, and EXIT_FAILURE. The latter two are macros defined in stdlib.h.

>int i,j,count=1,flag,arr[5],p[5];
Each variable should be declared on a separate line. This makes it easier to read, document, and maintain your code. It's also a good way to avoid bugs.

>scanf("%d",&arr);
Always check the return values of input functions. If you don't, you have no idea if they succeeded or not, and that makes your code extremely brittle.

>getch();
This is presumably why you included conio.h, and it's a lame reason. getch is a non-portable function and pausing the program is reasonably trivial with standard functions.

Finally, do you realize that you can put whitespace between tokens? It helps readability quite a bit.

>for(j=0;j<=count;j++)
This is where your code is broken. Test these sets of numbers: {1, 2, 2, 2, 2}, {1, 2, 1, 2, 1}. The problem is that you're overrunning the length of the result set by forgetting that any zero-indexed list goes from 0 to N-1, not 0 to N. Use j<count and it'll work better.

Here's my version of your code:

#include <stdio.h>

#define length(a) ( sizeof (a) / sizeof *(a) )

int main ( void )
{
  int unique = 0; /* The length of dst after removing duplicates */
  int n;          /* The length of src after loading numbers */
  int src[5];     /* The original set of numbers */
  int dst[5];     /* The set of numbers without duplicates */
  int i;

  /* Load up to N numbers into the source list */
  for ( n = 0; n < length ( src ); n++ ) {
    if ( scanf ( "%d", &src[n] ) != 1 )
      break;
  }

  /* The first number is never a duplicate */
  dst[unique++] = src[0];

  /* Load the unique numbers into the destination list */
  for ( i = unique; i < n; i++ ) {
    int has_dup = 0;
    int j;

    for ( j = 0; j < unique; j++ ) {
      if ( src[i] == dst[j] )
        has_dup = 1;
    }

    if ( has_dup == 0 )
      dst[unique++] = src[i];
  }

  /* Display the unique numbers */
  for ( i = 0; i < unique; i++ )
    printf ( "%d\t", dst[i] );
  printf ( "\n" );

  return 0;
}

There's really nothing wrong with your solution, just the implementation bug. However, the flag is a little kludgy when you can use the value of j instead. If j gets to the end of the list, a duplicate wasn't found. This simplifies the operation a smidge:

/* Load the unique numbers into the destination list */
for ( i = unique; i < n; i++ ) {
  int j;

  for ( j = 0; j < unique; j++ ) {
    if ( src[i] == dst[j] )
      break;
  }

  if ( j == unique )
    dst[unique++] = src[i];
}
commented: They set themselves up, you knock 'em down. Fun for the whole family. +11
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.