| | |
MERGED: Deleting duplicates in an array (plz help me out!!!!!!!)
![]() |
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
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......
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......
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.
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.
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
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......
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......
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
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?
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?
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[i] == a[j] unless you say "if ((i !=j) && (a[i] == 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!
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!
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Help with strings and file directory tree
- Next Thread: Fatal Error C010?
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






