Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a vb program and I would like to use list one values and check for duplicates from 2 lists.
The variable name for the values I am looking for in both lists is strCaseEventInternalID

List one strCaseEventInternalIDList have 3 values 1810458047, 1810458073 and 1810458074.

List two have 3 values 1810458073, 1810458074 and 1810458074
List two is in this object

VB
objXmlResponseDoc2 = Msc.Integration.Mncis.Library.v4.Odyssey.QueryDB(strSql, "Justice", False, True)


There is a duplicate (1810458074) in list two which is what I want to find.
How do I do that?

What I have tried:

VB
For Each strCaseEventInternalID In strCaseEventInternalIDList
    If (strCaseEventInternalIDList.Count() > 1) Then
	'Do something
    End If
Next
Posted
Updated 30-Jun-17 9:06am
v3
Comments
PIEBALDconsult 30-Jun-17 12:15pm    
Put each list in a Hashset and look at the union of them.

Not clear if your question is asking to find duplicate in a list or in between two list. Anyway, here are examples on how to accomplish both scenario
VB
 Dim l1 = New List(Of Integer) From {1810458047, 1810458073, 1810458074}
 Dim l2 = New List(Of Integer) From {1810458073, 1810458074, 1810458074}

'find item that exists in l1 but not l2
 Dim l3 = l1.Except(l2).ToList()  ' result is {1810458047}
 
'find item in both List
 Dim l4 = l1.Intersect(l2).ToList()  ' result is {1810458073,1810458074}

'find duplicate in a List
 Dim duplicates1 = l1.Where(Function(x) l1.Where(Function(y) x = y).Count() > 1).Distinct() 'result is NOTHING
 Dim duplicates2 = l2.Where(Function(x) l2.Where(Function(y) x = y).Count() > 1).Distinct() 'result is 1810458074

 'find duplicates item in l2 that exists in l1
 Dim l5 = l2.Intersect(l1).Where(Function(x) l2.Where(Function(y) x = y).Count() > 1).Distinct().ToList() 'result 1810458074
'find duplicates item in l1 that exists in l2
Dim l6 = l1.Intersect(l1).Where(Function(x) l1.Where(Function(y) x = y).Count() > 1).Distinct().ToList() 'result is NOTHING

.net - how to get duplicate items from a list in vb.net - Stack Overflow[^]

vb.net - Compare two lists and get difference - Stack Overflow[^]
 
Share this answer
 
v2
Comments
Member 11403304 30-Jun-17 10:38am    
I am trying to find if list two has duplicate values from list one
Bryian Tan 30-Jun-17 10:46am    
Then, how about something like

Dim l5 = l2.Intersect(l1).Where(Function(x) l2.Where(Function(y) x = y).Count() > 1).Distinct().ToList()
Member 11403304 30-Jun-17 11:31am    
So what do I do with my For each statement? Maybe my question is not clear? List 1 has 3 items and list 2 has 3 items but 2 of the three are duplicates from list one. i.e. 1810458074 from list one appears twice in list two. How do I use a For each and If statements to get that value?
Bryian Tan 30-Jun-17 12:23pm    
You can use the for each and linq combination instead of looping through each list. Perhaps I should ask if your application support Linq query, if not then the nested loops from other member should do the work.

Dim l1 = New List(Of Integer) From {1810458047, 1810458073, 1810458074}
        Dim l2 = New List(Of Integer) From {1810458073, 1810458074, 1810458074, 1810458074}

        For Each strCaseEventInternalID In l1
            If l2.FindAll(Function(f As Integer) f = strCaseEventInternalID).Count > 1 Then
                Console.WriteLine(strCaseEventInternalID & " is Duplicate")
            End If
        Next
If you want to use 'For Each' (and don't care if there are duplicates in list1 or list2 itself):
VB
For Each val1 As Integer In list1
    For Each val2 As Integer In list2
        If val1 = val2 Then
        ' found duplicate
        End If
    Next
Next

(Edit: added 'Then' with 'If')
 
Share this answer
 
v2
Comments
Member 11403304 30-Jun-17 13:10pm    
Thank you so much Peter. Your code with some tweaking is working. You made my day!
[no name] 30-Jun-17 13:16pm    
You're welcome. Glad you liked it!
Quote:
List two have 3 values 1810458073, 1810458074 and 1810458074
There is a duplicate (1810458074) in list two which is what I want to find.

I understand this as searching duplicates in a single list as the list contains 1810458074 2 times.
VB
Dim list1 = New List(Of Integer) From {1810458073, 1810458074, 1810458074}
For x = LBound(list1) to UBound(list1) - 1
  For y = x + 1 to UBound(list1)
    If list1(x) = list1(x) then
      ' found duplicate
    End If
  Next
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900