home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!elroy.jpl.nasa.gov!usc!cs.utexas.edu!torn!nott!cunews!revcan!micor!goss
- From: goss@micor.ocunix.on.ca (Timothy D. Goss)
- Subject: Re: Locating duplicates
- Organization: M.B. Cormier INC., Orleans (Ont)
- Date: Tue, 5 Jan 1993 04:34:56 GMT
- Message-ID: <1993Jan5.043456.29130@micor.ocunix.on.ca>
- References: <1993Jan2.041658.21596@organpipe.uug.arizona.edu> <DOUGM.93Jan2014723@titan.cs.rice.edu> <C07z6L.1Aq@world.std.com>
- Lines: 52
-
- You can locate ALL duplicates in a list of integers with a single pass of the
- list which is O(n) in time but it will require an array with 64K elements. The
- program below should explain...
-
- typedef unsigned char byte;
-
- static byte found[65536];
- int intlist[NUM];
-
- void main(void)
- {
-
- int i;
- int check;
-
- /* assume intlist is filled with NUM integers */
-
- for (i=0; i<NUM; i++) {
- check = intlist[i];
- if (found[check] > 0 && found[check] != 0xff) {
- printf("%d\n", check);
- found[check] = 0xff; /* don't report same dupe twice */
- } else {
- found[check] += 1; /* first time we saw this one */
- }
- }
-
- }
-
- Of course this program could be modified to use a hash table to store the found
- integers at a slight performance penalty. If memory is not a problem the 64K
- found array could be malloc'ed then free'd after.
-
- I haven't actuall run this program but the idea is what I want to get across.
- Sorting the list isn't really necessary, that's a very awkward way to solve
- that particular problem.
-
- For the really memory stingy you could you a bitmap for the found array but
- it would require 2 bits for each integer to avoid reporting more than 2
- occurrences of the same integer.
-
- I can't really see a faster way to do this with an unordered list, O(n) is the
- best you can hope for.
-
- +----------------------------------------------------------------------------+
- | _ /| |
- | \'o.O' We the unwilling, led by the unknowning, |
- | =(___)= are doing the impossible for the ungrateful. |
- | U We have done so much, for so long, with so little |
- | Timothy D. Goss we are now qualified to do anything with nothing. |
- | goss@micor.ocunix.on.ca |
- +----------------------------------------------------------------------------+
-