Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Prem Bhazee
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34974
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Prem Bhazee
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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".
 
Prem Bhazee
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experts,
Does the program below resolves the purpose? Please provide your input.

-Lave
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, is the output correct, or is there anything wrong with it?
 
Lave Kulshreshtha
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic