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: 35

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
Top