User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,663 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,500 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 13494 | Replies: 49
Reply
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

deleting duplicates in array

  #1  
Oct 28th, 2004
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......
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: deleting duplicates in array

  #2  
Oct 28th, 2004
could any 1 reply plz............
Reply With Quote  
Join Date: Sep 2004
Posts: 6,325
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: deleting duplicates in array

  #3  
Oct 28th, 2004
>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]
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: deleting duplicates in array

  #4  
Oct 28th, 2004
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......
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: deleting duplicates in array

  #5  
Oct 28th, 2004
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......
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: deleting duplicates in array

  #6  
Oct 29th, 2004
Originally Posted by galmca
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.
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Sep 2004
Posts: 6,325
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: deleting duplicates in array

  #7  
Oct 29th, 2004
>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. Anyway, it's best to just ignore people like galmca. They go away after a short time and leave us in peace.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 9
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: deleting duplicates in array

  #8  
Oct 28th, 2004
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[i] 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[i] 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[i] over to nonDuplicates[countOfNonDuplicates] and increment the countOfNonDuplicates.
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: deleting duplicates in array

  #9  
Oct 29th, 2004
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[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......
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: deleting duplicates in array

  #10  
Oct 29th, 2004
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[i]==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?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 1:57 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC