M magarcan Active Member Licensed User Longtime User Aug 18, 2018 #1 Hi there!! I've two lists with Strings as Objects. I've to compare which elements are in both Lits. I've thoung in make two anidated For loops but don't know if are there a better way to do it. Any suggestion? Cheers!
Hi there!! I've two lists with Strings as Objects. I've to compare which elements are in both Lits. I've thoung in make two anidated For loops but don't know if are there a better way to do it. Any suggestion? Cheers!
LucaMs Expert Licensed User Longtime User Aug 18, 2018 #2 magarcan said: don't know if are there a better way to do it. Click to expand... No, because they are of different types. If they were the same, you could just loop on one list and use IndexOf method of the second one. Upvote 0
magarcan said: don't know if are there a better way to do it. Click to expand... No, because they are of different types. If they were the same, you could just loop on one list and use IndexOf method of the second one.
stevel05 Expert Licensed User Longtime User Aug 18, 2018 #3 I would think the simplest would be something like: Code: Copy Dim L1 As List = Array(1,2,3,4,8) Dim L2 As List = Array(3,6,7,8) Dim Results As List Results.Initialize For Each O As Object In L1 If L2.IndexOf(O) > -1 Then Results.Add(O) Next For Each O As Object In Results Log(O) Next Upvote 0
I would think the simplest would be something like: Code: Copy Dim L1 As List = Array(1,2,3,4,8) Dim L2 As List = Array(3,6,7,8) Dim Results As List Results.Initialize For Each O As Object In L1 If L2.IndexOf(O) > -1 Then Results.Add(O) Next For Each O As Object In Results Log(O) Next
M magarcan Active Member Licensed User Longtime User Aug 18, 2018 #4 Here goes my code: Code: Copy 'Compare 2 lists For x = 0 To CountryList1.Size-1 For y = 0 To CountryList2.Size-1 If (CountryList1.Get(x) == CountryList2.Get(y)) Then Log(CountryList1.Get(x) & " are in common") End If Next Next Upvote 0
Here goes my code: Code: Copy 'Compare 2 lists For x = 0 To CountryList1.Size-1 For y = 0 To CountryList2.Size-1 If (CountryList1.Get(x) == CountryList2.Get(y)) Then Log(CountryList1.Get(x) & " are in common") End If Next Next
LucaMs Expert Licensed User Longtime User Aug 18, 2018 #5 magarcan said: I've two lists with Strings as Objects. I've to compare which elements are in both Lits. Click to expand... 1) You cannot compare Strings and Objects 2) You could not compare two objects, you should compare their content (unless you want to know if they are the same object or, better, the same reference to the same object) 3) This is not B4: magarcan said: == Click to expand... magarcan said: For x = 0 To CountryList1.Size-1 For y = 0 To CountryList2.Size-1 Click to expand... if both CountryList1 and CountryList2 are lists of strings: Code: Copy For Each Country As String In CountryList1 If CountryList2.IndexOf(Country) <> - 1 Then Log(Country & " is also in CountryList2") End If Next Upvote 0
magarcan said: I've two lists with Strings as Objects. I've to compare which elements are in both Lits. Click to expand... 1) You cannot compare Strings and Objects 2) You could not compare two objects, you should compare their content (unless you want to know if they are the same object or, better, the same reference to the same object) 3) This is not B4: magarcan said: == Click to expand... magarcan said: For x = 0 To CountryList1.Size-1 For y = 0 To CountryList2.Size-1 Click to expand... if both CountryList1 and CountryList2 are lists of strings: Code: Copy For Each Country As String In CountryList1 If CountryList2.IndexOf(Country) <> - 1 Then Log(Country & " is also in CountryList2") End If Next
LucaMs Expert Licensed User Longtime User Aug 18, 2018 #6 I would prefer lstCountry1 as name Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Aug 19, 2018 #7 If the lists are long then it is better to convert one of them to a Map. List.IndexOf must go over all items until it finds a match - O(n). Map.ContainsKey on the other hand is very fast - O(1). Upvote 0
If the lists are long then it is better to convert one of them to a Map. List.IndexOf must go over all items until it finds a match - O(n). Map.ContainsKey on the other hand is very fast - O(1).