how to u remove duplicates from an array of integers
4 1618
i think u already captured how to populate array .....
look at this code carefully ..... - int a[] = {x,y,.......10th};
-
int count = 10;
-
-
for(int i = 0;i<count;i++)
-
{
-
for(int j=count-1;j>i;j++) //Check duplicate
-
{
-
if(a[j] == a[i]) //Duplicate found
-
{
-
for(int k=i;k<count-1;k++)
-
{
-
a[k]=a[k+1];
-
count++;
-
}
-
}
-
}
-
}
with out real test i send u the code plz verify it and contact with me ......
best of luck
dmjpro-
after running your code, my compiler threw a segfault, I'm pretty sure it's because of your j++ at the end of the second for loop.
holla-
I would recommend going through and finding out how many duplicates there are
pseudocode:
take the first compare it to all the rest
if it's a duplicate,
increment duplicateCounter
take the second and compare it to the third and on (it's already been comapred to the first)
if it's a duplicate,
increment duplicateCounter
take the third and compare it to the fourth and on
....
subtract duplicateCounter from ten
create an array with that many places
populate everything but the duplicates
You could always also just create an array of the same size, fill it with values that you know won't ever occur (if you do know what values will occur) like -1, and then just copy anything that's not a duplicate - that removes the whole process of finding the size of the new array, and then you could just print it out until you hit that value... but that's only if you know that you're not going to ever have an element with that number.
Hey bro, here's some code I just wrote to illustrate.
The code is not fancy as I tried to make it very 'step-by-step' mode and wrote it quickly.
There is a single comment on it.
If you need further comments on it to help you understand, please mail me.
Kind regards,
Fabiano.
//---------------------------------------------------- -
#include <stdio.h>
-
#include <conio.h>
-
-
#define ARRAYMAXSIZE 10
-
-
int main(void)
-
{
-
int array1[ARRAYMAXSIZE] = {5, 4, 1, 3, 2, 4, 3, 2, 5, 5},
-
array2[ARRAYMAXSIZE] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
-
count1 = 0, count2 = 0, alreadyexists = 0, *frompointer, *topointer, *checkpointer;
-
-
clrscr();
-
printf("\n\n\n");
-
-
frompointer = &array1[0];
-
topointer = &array2[0];
-
checkpointer = &array2[0];
-
-
for(count2 = 0; count2 < ARRAYMAXSIZE; count2++, frompointer++)
-
{
-
checkpointer = &array2[0];
-
alreadyexists = 0;
-
for(count1 = 0; count1 < ARRAYMAXSIZE; count1++, checkpointer++)
-
{
-
if(*frompointer == *checkpointer)
-
{
-
alreadyexists = 1;
-
break; // no sense in continue checking for duplicates if you already found one
-
}
-
}
-
if(!alreadyexists)
-
{
-
*topointer = *frompointer;
-
topointer++;
-
}
-
}
-
for(count1 = 0; count1 < ARRAYMAXSIZE; count1++)
-
{
-
if(array2[count1] > 0)
-
printf("%d ", array2[count1]);
-
}
-
return 0;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michelle |
last post by:
hi,
i have created an array from recordset containing user names
eg. (davidp, davidp, evenf, patricka, rebeccah)
which i have sorted in alphabetical order, but i need to
identify duplicates...
|
by: Marlene |
last post by:
Hi All
I have the following scenario, where I have found all the duplicates
in a table, based on an order number and a part number (item).I might
have something like this:
Order PODate Rec...
|
by: tyrfboard |
last post by:
I've been searching for awhile now on how to remove duplicates from a
table within an Access db and have found plenty of articles on finding
or deleting duplicates. All I want to do is remove them...
|
by: AK |
last post by:
Hi
Our product uses MS-SQL Server 2000. One of our customer has 10
installations with each installation stroring data in its own database.
Now the customer wants to consolidate these databases...
|
by: vsgdp |
last post by:
I have a container of pointers. It is possible for two pointers to point to
the same element. I want to remove duplicates.
I am open to which container is best for this.
I thought of using...
|
by: ak |
last post by:
Is it possible to find repeated(duplicate) element in an array in
single loop ?
AK
|
by: ryan.paquette |
last post by:
In the table there are 2 fields in which I wish to limit (i.e. No
Duplicates)
Although I do not want to limit them to "No Duplicates" separately.
I need them to be limited to "No Duplicates" as...
|
by: john.cole |
last post by:
I have searched all the groups I can, and I still haven't been able to
come up the solution I need. I have the following problem.
In my form named sbfrmSpoolList, I am entering a job, spool and...
|
by: tskmjk55 |
last post by:
Recently, I have a requirement to develop a vb.net application wherein the input excel sheet data which has an average of 5000 records should be checked for Internal duplicates (duplicates within the...
|
by: Thekid |
last post by:
I'm trying to figure out a way to find if there are duplicates in an array. My idea was to take the array as 'a' and make a second array as 'b' and remove the duplicates from 'b' using 'set' and then...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |