Link to home
Start Free TrialLog in
Avatar of gopikrish
gopikrish

asked on

Comparing a list to another list and getting difference?

I am comparing a list of items say ("Apple", "Orange", "Banana") to another list ("Apple", "Orange")
So I need to get "Banana" alone. Other than looping through items and comparing, is there any easy way to find it out in vb ? Thanks.
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"Other than looping through items and comparing, is there any easy way to find it out in vb ?"

No, there isn't.  BlueDevilFan has the right idea though using collections.  Searching for an item by key in a collection is far faster than using the typical double nested loop necessary to check two arrays.

One quick comment however.  In your original lists, only one set had a differenece (meaning one set was a subset of the other).  What if your lists are like the below where there are differences on both sides?

    ("Apple", "Orange", "Banana")
    ("Apple", "Orange", "Grapes")

Would you then want both "Banana" and "Grapes"?  BlueDevilFan's algorithm will work for the case you gave but would not catch "Grapes".

To fix this you have two options:
 
    (1) Remove matches from the second collection as they are found.  After iterating the first collection, anything left in the second collection should also be added to your differences collection.  Of course, this modifies the second collection and may not be desireable.

    (2) After iterating the first collection, iterate the second collection to find the items in it that don't exist in the first collection.  This would look exactly like the code already given to you but with the collections reversed.  Even though we iterate both collections entirely, this would still be faster than using double nested loops with arrays.

~IM
Good catch, IM!
Avatar of gopikrish
gopikrish

ASKER

Ok sorry for the delay. Thanks But I am having a small problem. I am doing like this,

Do While Not rs.EOF
    Set colList1 = MakeCollection(rs(0))
    rs.MoveNext
 Loop

Do While Trim(.Cells(rowcounter, 1).Text) = combo
    Set colList2 = MakeCollection(Trim(.Cells(rowcounter, 2).Text))
    rowcounter = rowcounter + 1
Loop

Private Function MakeCollection(varItem As Variant) As Collection
Dim colTemp As New Collection
colTemp.Add varItem, varItem
Set MakeCollection = colTemp
End Function

So when I call  Set colDiff = CompareLists(colList1, colList2) ,
my previous values in colList1 and colList2 are getting removed. So how to store all values in colList1 and colList2 even after that Do looping when I call MakeCollection function?
Thanks.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok Thanks a lot !
Now I am bit confused to whom I should award points :(
Is it possible to split points and assign to both? :)
If not I will create another question and assign that to one of the two?
Yes...you can split points:
https://www.experts-exchange.com/help.jsp#hi69

    "You split the points. Scroll down to the bottom of the question and click the "Split Points" link at the bottom of the page. Select the radio button of the comment who you want to Accept as the answer. Only one button can be selected. Set the point value (a text box above the comment) of how much you want this person to receive of the points. Then set the point values for each of the experts comments to whom you want to allocate points and these will be considered Assisted answers in helping you resolve the issue. Double check your information and then click the Submit button at the bottom of the page. One note: the total points of the splits must equal the amount you asked the question for itself, and no person can receive fewer than 20 points."
Ok and sorry one more problem came...
The logic you gave is getting all the items from List1 irrespective of the items in List2

For Each varItem In colList1
varTest = colList2.Item(varItem)
If Err.Number = 5 Then
colTemp.Add varItem, varItem
Err.Clear
End If

Suppose List1 = ( "Apple", "Banana", "Orange")  List2 = ( "Banana")

Then colTemp is getting ( "Apple", "Banana", "Orange")

So its not omiting "Banana". Is that condition varTest = colList2.Item(varItem) correct? or Err.Number = 5 correct?
Thanks.
Sorry its working now when I manually added Items into collection. Thanks again !