For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2007 > removing duplicates from an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author removing duplicates from an array
Mad_man_on_a_MissIon

2007-01-20, 7:09 pm

I'm trying to figure out how to remove duplicates from an array, of
course this is a lot easier if you sort the array first, but what if I
need to preserve the order of the array?

Casey Hawthorne

2007-01-20, 7:09 pm

Scan through and store entries along with indicies into a hash
map/dictionary.

"Mad_man_on_a_MissIon" <madmax138@hotmail.com> wrote:

>I'm trying to figure out how to remove duplicates from an array, of
>course this is a lot easier if you sort the array first, but what if I
>need to preserve the order of the array?

--
Regards,
Casey
dimo414

2007-01-22, 8:20 am

Since you're using an array, and not an ArrayList, I'm assuming you're
doing some APCS homework. So first off, DO YOUR OWN HOMEWORK!

If you are in fact not a student, I'm sorry, but you shouldn't be using
an array if you're trying to remove lots of items.

Now, I assume somewhere java has a merge/quick sort method and a binary
search method (as an APCS student, I built my own - but there must be
built in ones somewhere). So once you've found those methods (or made
your own) here's basicly what you do.

Create a second array list which will store all the values found so
far. Iterate through your first ArrayList, use binary search for the
value in the second ArrayList (since the second ArrayList is empty in
the begining, it won't find anything) and if it's not found, add the
value into the second ArrayList and sort it. If the search /does/ find
anything, that means it already apeared in the first ArrayList, so
remove it.

Of course, if the array your cleaning up is particuarly small, it may
be faster to simply use a linear search of the second ArrayList and not
bother sorting it each time.

Hope that helps!

Lew

2007-01-22, 8:20 am

dimo414 wrote:
> Create a second array list which will store all the values found so
> far. Iterate through your first ArrayList, use binary search for the
> value in the second ArrayList (since the second ArrayList is empty in
> the begining, it won't find anything) and if it's not found, add the
> value into the second ArrayList and sort it. If the search /does/ find
> anything, that means it already apeared in the first ArrayList, so
> remove it.
>
> Of course, if the array your cleaning up is particuarly small, it may
> be faster to simply use a linear search of the second ArrayList and not
> bother sorting it each time.


public void elimDupes( List list )
{
Set noDupes = new HashSet();
noDupes.addAll( list );
list.clear();
list.addAll( noDupes );
}

Untested. Unlikely to preserve order, although that's easily mended.

- Lew
Rhino

2007-01-22, 8:20 am


"Mad_man_on_a_MissIon" <madmax138@hotmail.com> wrote in message
news:1169336288.290184.127980@q2g2000cwa.googlegroups.com...
> I'm trying to figure out how to remove duplicates from an array, of
> course this is a lot easier if you sort the array first, but what if I
> need to preserve the order of the array?
>


Perhaps you have the cart before the horse.

Rather than taking duplicates out of the array after they have already been
put there, why not change the code that populates the array to prevent
duplicates from being stored in the first place? Therefore, if the key is
phone number and you want to prevent duplicate entries, look at the key of
the incoming record and see if it matches any record already in the array;
if it does, display a message to the effect that the new record is a
duplicate and will be rejected. (Or, turn the new record into an update of
the existing record rather than an insert of a new record.)

--
Rhino


Mad_man_on_a_MissIon

2007-01-22, 8:20 am


dimo414 wrote:
> Since you're using an array, and not an ArrayList, I'm assuming you're
> doing some APCS homework. So first off, DO YOUR OWN HOMEWORK!


This is a very, very small part of my homework, I did the rest of it
myself, ..... as a matter of fact I did all of it myself since I
figured this out on my own. This was just a safety net so to speak. I
am not an APCS, I am a second year CS student (with a 3.8 G.P.A.). If
you think that you are going to do everything yourself in college w/o
getting help from your peers or professors, you are going to have a
very difficult time.

anyways .... thanks for the help guys.

ass|u|me.

printdude1968@gmail.com

2007-01-22, 8:20 am


> Rather than taking duplicates out of the array after they have already been
> put there, why not change the code that populates the array to prevent
> duplicates from being stored in the first place?


Does ArrayList handle this better than an array. Is there another
datastructure better suited for storing non-duplicate items?

Lew

2007-01-22, 8:20 am

printdude1968@gmail.com wrote:
> Is there another
> datastructure better suited for storing non-duplicate items?


java.util.Set

- Lew
Oliver Wong

2007-01-22, 7:08 pm


"Mad_man_on_a_MissIon" <madmax138@hotmail.com> wrote in message
news:1169409845.174359.214560@v45g2000cwv.googlegroups.com...
>
> dimo414 wrote:
>
> This is a very, very small part of my homework, I did the rest of it
> myself, ..... as a matter of fact I did all of it myself since I
> figured this out on my own. This was just a safety net so to speak. I
> am not an APCS, I am a second year CS student (with a 3.8 G.P.A.). If
> you think that you are going to do everything yourself in college w/o
> getting help from your peers or professors, you are going to have a
> very difficult time.


We're not your peers nor your professors, though.

- Oliver


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com