aspose file tools*
The moose likes Java in General and the fly likes Eliminating Duplicate in Arraylist and moving the dups to another arraylist. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Eliminating Duplicate in Arraylist and moving the dups to another arraylist." Watch "Eliminating Duplicate in Arraylist and moving the dups to another arraylist." New topic
Author

Eliminating Duplicate in Arraylist and moving the dups to another arraylist.

Prem Bhazee
Ranch Hand

Joined: Feb 18, 2006
Posts: 34
Hi Ranchers,
Can you please help me with this problem.

I have a master arraylist say A1 which hase values like
Red,
Blue,
Green

I have to copy unique/non duplicate values from another arraylist say A2 to A1. Whenever there is a duplicate value in A2 which corresponds to A1 the value should be stored in another arraylist A3.

This is what I have come up with.

alclone is the clone of the master arraylist say A1 here it is alList.
alTempList is the arraylist A2.
alTempDupList is the duplicate arraylist A3.
alList is the master list.



The problem I am facing here is that if A2 also has values like
Red,
Blue

In the second iteration when Red is being compared with Blue it is being added to the master list. So I am getting

A1

Red,
Blue,
Green
Blue

Blue is getting added to the master arraylist which again creates duplicate even though red is eliminated. Not sure is this the right approach or is there any other approach which can be taken

Thanks in advance.

[ January 20, 2008: Message edited by: Prem Bhazee ]

[ January 20, 2008: Message edited by: Prem Bhazee ]
[ January 20, 2008: Message edited by: Prem Bhazee ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29274
    
140

Prem,
Is this for school? If not, consider using the built in HashSet to deal with duplicates for you rather than implementing complex logic.

Either way, are you saying that A2 contains "Red, Blue" as a single element or as two different elements?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Prem Bhazee
Ranch Hand

Joined: Feb 18, 2006
Posts: 34
Jeanne,
Thanks for the quick reply. A2 contains two different elements Red and Blue.
I am supposed to use arraylist for this task. Can you think of any other approach for this task using arraylist.

Thanks in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

First of all, the names that you have chosen for the variables are just bad -- I don't know why, but I got a headache just trying to figure out what is what.

The problem is the inner "else" statement. You make the assumption that if one entry from A1 (clone) doesn't match one entry from A2, then you can add the entry into A1. This is not true, you have to check the item (from A2) with every item in A1 (or clone of), before you can add it to the A1.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Henry]: First of all, the names that you have chosen for the variables are just bad -- I don't know why, but I got a headache just trying to figure out what is what.

Yes, I was about to post the same thing. Try using names that are short but communicate the key differences you need to know. Maybe the original list can be "originalList" (or just "list"), and the other two lists can be called "noDuplicates" and "duplicates".


"I'm not back." - Bill Harding, Twister
Prem Bhazee
Ranch Hand

Joined: Feb 18, 2006
Posts: 34
Henry/Jim

Thanks for your replies. I have come up with a logic.

Do you think that if I remove the else part completely and in the if block if I encounter a DUP then remove the dup element from the A2(alTempList) list .So when I finally come out of the for loops A2 will contain all Non-dup values and then add this to main list. A3 will have dup values(alTempDupList).
Pseudo code Like this

if (strYescloneName.equalsIgnoreCase(strYesTempName)) {alTempDupList.add((String) alTempList.get(jl)); alTempList.remove(jl);

Not sure does this work haven't tested.

Thanks in advance.
[ January 20, 2008: Message edited by: Prem Bhazee ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

Thanks for your replies. I have come up with a logic.


While it is an interesting idea -- and it may work. You know it does feel like a band-aid over an overly bandaged area. Here is another option... You do know that the arraylist has an contains() method, that can be used to check if an item exists, right?

With this, you can (in pseudo code)....



Henry
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
One issue to beware of is that contains() will check for exact equality. Prem, your code so far shows you're trimming the strings and using equalsIgnoreCase() to compare. If that's necessary, then since contains() won't do that, you would need to make sure you trim the strings and convert them to all lowercase (or all uppercase if you prefer) before you put them into what Henry calls the masterList. (I still can't read those names Prem uses - too long, confusing, and uninformative.) Using contains() can make your code simpler here, but you do need to be careful how you do it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

I still can't read those names Prem uses - too long, confusing, and uninformative


Jim, it is actually quite simple.

The master list is the alList. There is also a clone of this master list called the alclone. For this question, we will call both of these lists as A1. The new items lists is the alTempList. We will simplify this by calling this list A2. The duplicate list is the alTempDupList. We will simplify this by calling this list A3.

strYesCloneName is an element from A1, aka the master list, or specifically the clone of the master list, the alclone list. strYesTempName is an element from A2, aka the new items list, or specifically the altemp list.

Now... if there is a duplicate of these two variables, then that means that an item from alclone equals an item from altemp, which means that an item from A1 equals an item from A2, which means an item from the new items list equals an item from the master list (or at least, a clone of the master list).... ahhh .... hmmmm...

Okay, let me get an asprin. I'll finish this later....

Henry
Lave Kulshreshtha
Ranch Hand

Joined: Oct 21, 2007
Posts: 106
Experts,
Does the program below resolves the purpose? Please provide your input.

-Lave


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, ITIL V3 Foundation Certification
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I am happy with the shorter names at least. Thank you.

What can you tell from looking at the output? Is the result what you expect? Is it OK? That's probably the most important consideration right now. I can see several ways to make the program more efficient, but I think the first priority should be to make sure the result is correct. What does it look like to you?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
One thing I can't tell from the original problem - can there be duplicates within a list? For example, can al contain "5" twice? If so, should "5" appear on the list of duplicates? Or is that only for things that are in both al and al2?
Lave Kulshreshtha
Ranch Hand

Joined: Oct 21, 2007
Posts: 106
Hi here is the result of program

List one: [1, 2, 5, 6, 3, 4]
List two [1, 2, 7, 8]
Dup values [1, 2]
List with all values [3, 2, 1, 7, 6, 5, 4, 8]

Can you please suggest ways to improve program.

-Lave

Originally posted by Jim Yingst:
I am happy with the shorter names at least. Thank you.

What does it look like to you?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, is the output correct, or is there anything wrong with it?
Lave Kulshreshtha
Ranch Hand

Joined: Oct 21, 2007
Posts: 106
Output is correct


Originally posted by Jim Yingst:
Well, is the output correct, or is there anything wrong with it?

[ January 23, 2008: Message edited by: Lave Kulshreshtha ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Eliminating Duplicate in Arraylist and moving the dups to another arraylist.
 
Similar Threads
Garbage collection query
Garbage collection question
Array
Dan Chisholm's Array Question
removing duplicates from the list?