473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

duplicates

22
how to u remove duplicates from an array of integers
Mar 15 '07 #1
4 1618
dmjpro
2,476 2GB
i think u already captured how to populate array .....

look at this code carefully .....

Expand|Select|Wrap|Line Numbers
  1. int a[] = {x,y,.......10th};
  2. int count = 10;
  3.  
  4. for(int i = 0;i<count;i++)
  5. {
  6.    for(int j=count-1;j>i;j++) //Check duplicate
  7.    {
  8.       if(a[j] == a[i]) //Duplicate found
  9.       {
  10.          for(int k=i;k<count-1;k++)
  11.          {
  12.             a[k]=a[k+1];
  13.             count++;
  14.          }
  15.       }
  16.    }
  17. }
with out real test i send u the code plz verify it and contact with me ......

best of luck
Mar 15 '07 #2
sicarie
4,677 Expert Mod 4TB
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.
Mar 15 '07 #3
dmjpro
2,476 2GB
sooryy... my mistake
Mar 15 '07 #4
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.
//----------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. #define ARRAYMAXSIZE 10
  5.  
  6. int main(void)
  7. {
  8.     int array1[ARRAYMAXSIZE] = {5, 4, 1, 3, 2, 4, 3, 2, 5, 5},
  9.         array2[ARRAYMAXSIZE] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  10.         count1 = 0, count2 = 0, alreadyexists = 0, *frompointer, *topointer, *checkpointer;
  11.  
  12.     clrscr();
  13.         printf("\n\n\n");
  14.  
  15.     frompointer = &array1[0];
  16.     topointer = &array2[0];
  17.     checkpointer = &array2[0];
  18.  
  19.     for(count2 = 0; count2 < ARRAYMAXSIZE; count2++, frompointer++)
  20.     {
  21.         checkpointer = &array2[0];
  22.         alreadyexists = 0;
  23.         for(count1 = 0; count1 < ARRAYMAXSIZE; count1++, checkpointer++)
  24.         {
  25.             if(*frompointer == *checkpointer)
  26.             {
  27.                 alreadyexists = 1;
  28.                 break; // no sense in continue checking for duplicates if you already found one
  29.             }
  30.         }
  31.         if(!alreadyexists)
  32.         {
  33.             *topointer = *frompointer;
  34.             topointer++;
  35.         }
  36.     }
  37.     for(count1 = 0; count1 < ARRAYMAXSIZE; count1++)
  38.     {
  39.         if(array2[count1] > 0)
  40.             printf("%d ", array2[count1]);
  41.     }
  42.     return 0;
  43. }
  44.  
Mar 15 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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...
6
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...
16
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...
3
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...
7
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...
14
by: ak | last post by:
Is it possible to find repeated(duplicate) element in an array in single loop ? AK
3
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...
7
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...
1
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...
3
Thekid
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.