Android Question Recursion

Sergey_New

Well-Known Member
Licensed User
Longtime User
Please help me remove duplicate entries from the list using recursion or otherwise.
B4X:
Sub Globals
    Private lst As List
    Type record(c1, c2 As String)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    lst.Initialize
    Dim r As record
    r.c1="i"
    r.c2="y"
    lst.add(r)
    For i=0 To 1
        Dim r As record
        r.c1="i" & i
        r.c2="y" & i
        lst.add(r)
    Next
    Dim r As record
    r.c1="i"
    r.c2="y"
    lst.add(r)
End Sub
 

Attachments

  • temp.zip
    3.3 KB · Views: 55

JohnC

Expert
Licensed User
Longtime User
I don't know if I am fully understanding your goal, but if you simply want to make sure that there are no duplicates in the list, then ChatGPT generated this code:

B4X:
Sub Globals
    Private lst As List
    Type record(c1, c2 As String)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    lst.Initialize
    Dim r As record
    r.c1="i"
    r.c2="y"
    AddRecordToList(r)

    For i=0 To 1
        Dim r As record
        r.c1="i" & i
        r.c2="y" & i
        AddRecordToList(r)
    Next

    Dim r2 As record ' Renamed this one to avoid duplicate variable declaration
    r2.c1="i"
    r2.c2="y"
    AddRecordToList(r2)
End Sub

Sub AddRecordToList(r As record)
    For Each rec As record In lst
        If rec.c1 = r.c1 And rec.c2 = r.c2 Then
            Return ' Don't add duplicate
        End If
    Next
    lst.Add(r) ' If no duplicate found, add the record to the list
End Sub
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I don't know if I am fully understanding your goal
I need to remove identical entries from the created list. In this case, it is the first and last entry.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
My first thought is to add all the List items to a Map
Thanks, it works!
B4X:
    Dim mp As Map
    mp.Initialize
    For i=0 To lst.Size-1
        r=lst.get(i)
        mp.Put(r.c1 & r.c2 ,r)
    Next
    lst.Clear
    For i=0 To mp.Size-1
        lst.Add(mp.GetValueAt(i))
    Next
 
Upvote 1
Top