Removing duplicates

Reply

Join Date: Oct 2007
Posts: 1
Reputation: Bikram is an unknown quantity at this point 
Solved Threads: 0
Bikram Bikram is offline Offline
Newbie Poster

Re: deleting duplicates in array

 
0
  #1
Oct 1st, 2007
helos...!!!
wel am new to dis..!!!
but i hav a code to it...!!!
lets try out..!!!!!
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int i,j,count=1,flag,arr[5],p[5];
  6.  
  7. for(i=0;i<5;i++)
  8. scanf("%d",&arr[i]);
  9.  
  10. p[0]=arr[0];
  11. for(i=1;i<5;i++)
  12. {
  13. flag=0;
  14. for(j=0;j<=count;j++)
  15. {
  16. if(arr[i]==p[j])
  17. flag=1;
  18. }
  19.  
  20. if(flag==0)
  21. {
  22.  
  23. p[count]=arr[i];
  24. count++;
  25. }
  26. }
  27. for(i=0;i<count;i++)
  28. {
  29. printf("%d\t",p[i]);
  30. }
  31. getch();
  32. }
try dis out dude..!!!
Last edited by Narue; Oct 1st, 2007 at 11:15 am. Reason: Added code tags and uniform indentation.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 4
Reputation: prabhat padhy is an unknown quantity at this point 
Solved Threads: 0
prabhat padhy prabhat padhy is offline Offline
Newbie Poster

Re: deleting duplicates in array

 
0
  #2
Oct 1st, 2007
Originally Posted by galmca View Post
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......

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Removing duplicates

 
1
  #3
Oct 1st, 2007
>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 <integer value>;
}
and
int main ( int argc, char *argv[] )
{
  return <integer value>;
}
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[i]);
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:
  1. #include <stdio.h>
  2.  
  3. #define length(a) ( sizeof (a) / sizeof *(a) )
  4.  
  5. int main ( void )
  6. {
  7. int unique = 0; /* The length of dst after removing duplicates */
  8. int n; /* The length of src after loading numbers */
  9. int src[5]; /* The original set of numbers */
  10. int dst[5]; /* The set of numbers without duplicates */
  11. int i;
  12.  
  13. /* Load up to N numbers into the source list */
  14. for ( n = 0; n < length ( src ); n++ ) {
  15. if ( scanf ( "%d", &src[n] ) != 1 )
  16. break;
  17. }
  18.  
  19. /* The first number is never a duplicate */
  20. dst[unique++] = src[0];
  21.  
  22. /* Load the unique numbers into the destination list */
  23. for ( i = unique; i < n; i++ ) {
  24. int has_dup = 0;
  25. int j;
  26.  
  27. for ( j = 0; j < unique; j++ ) {
  28. if ( src[i] == dst[j] )
  29. has_dup = 1;
  30. }
  31.  
  32. if ( has_dup == 0 )
  33. dst[unique++] = src[i];
  34. }
  35.  
  36. /* Display the unique numbers */
  37. for ( i = 0; i < unique; i++ )
  38. printf ( "%d\t", dst[i] );
  39. printf ( "\n" );
  40.  
  41. return 0;
  42. }
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:
  1. /* Load the unique numbers into the destination list */
  2. for ( i = unique; i < n; i++ ) {
  3. int j;
  4.  
  5. for ( j = 0; j < unique; j++ ) {
  6. if ( src[i] == dst[j] )
  7. break;
  8. }
  9.  
  10. if ( j == unique )
  11. dst[unique++] = src[i];
  12. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC