Android Question How to identify duplicate elements in a list, from code

WebQuest

Active Member
Licensed User
Hello Community.
I can't identify via a comparison if there are equal elements in a list.

Which is a simple code to identify the duplicate and remove it from the list?.
B4A:
(ArrayList) [0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 35, 36, 36, 37, 38, 38, 38, 39, 39, 39, 40, 41, 41, 41, 42, 43, 45, 46, 50, 50, 50, 50]
 

JohnJ

Member
Licensed User
Longtime User
Search for "duplicates in list" in the forum. You will find a couple of suggestions.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Tested in B4J but should work in B4A too
B4X:
Sub Process_Globals
    Dim myset As JavaObject
End Sub

Sub AppStart (Args() As String)
    Dim myList As List
    myList.Initialize
    myList.AddAll(Array(0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 35, 36, 36, 37, 38, 38, 38, 39, 39, 39, 40, 41, 41, 41, 42, 43, 45, 46, 50, 50, 50, 50))

    myset.InitializeNewInstance("java.util.HashSet",Null)

    For Each value As Object In myList
        If myset.RunMethod("add",Array(value)) = False Then
            Log("Value [" & value & "] is duplicated")
        End If
        
    Next
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
to remove the duplicates - just add this to the above code before the End Sub
B4X:
    myList.Clear
    
    Dim iterator As JavaObject
    iterator = myset.RunMethod("iterator",Null)
    Do While iterator.RunMethod("hasNext",Null) 
        myList.Add(iterator.RunMethod("next",Null))
    Loop
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
simple code to identify the duplicate and remove it from the list?.
Or you can do something like this without JavaObject:
B4X:
Sub RemoveDuplicates(NyList As List) As List
    If NyList = Null Or Not(NyList.IsInitialized) Then Return NyList
    Dim lstNew As List
    lstNew.Initialize
    For Each objItem As Object In NyList
        If lstNew.IndexOf(objItem) = - 1 Then
            lstNew.Add(objItem)
        End If
    Next
    Return lstNew
End Sub
To test it do this:
B4X:
Dim firstlist As List = Array(0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 35, 36, 36, 37, 38, 38, 38, 39, 39, 39, 40, 41, 41, 41, 42, 43, 45, 46, 50, 50, 50, 50)
    Log(RemoveDuplicates(firstlist))
 
Upvote 1

WebQuest

Active Member
Licensed User


This worked for me. Thank you
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
B4X:
Sub AppStart (Args() As String)
    Dim firstlist As List = Array(0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 35, 36, 36, 37, 38, 38, 38, 39, 39, 39, 40, 41, 41, 41, 42, 43, 45, 46, 50, 50, 50, 50)
    Dim Now As Long = DateTime.Now
    Log(RemoveDuplicates(firstlist))
    Log(Abs(DateTime.Now - Now))
    Dim Now As Long = DateTime.Now
    Log(RemoveDuplicates2(firstlist))
    Log(Abs(DateTime.Now - Now))
End Sub

Sub RemoveDuplicates(NyList As List) As List
    If NyList = Null Or Not(NyList.IsInitialized) Then Return NyList
    Dim lstNew As List
    lstNew.Initialize
    For Each objItem As Object In NyList
        If lstNew.IndexOf(objItem) = - 1 Then
            lstNew.Add(objItem)
        End If
    Next
    Return lstNew
End Sub

Sub RemoveDuplicates2(MyList As List) As List
    If MyList = Null Or Not(MyList.IsInitialized) Then Return MyList
    If MyList.Size <= 1 Then Return MyList
  
    Dim lstNew As List
    lstNew.Initialize
  
    MyList.Sort(True)
      
    Dim PrevItem As Object = MyList.Get(0)
    lstNew.Add(PrevItem)
    For Each objItem As Object In MyList
        If PrevItem <> objItem Then
            lstNew.Add(objItem)
        End If
        PrevItem = objItem
    Next
    Return lstNew
End Sub

Debug result:
Waiting for debugger to connect...
Program started.
(ArrayList) [0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 50]
3
(ArrayList) [0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 50]
1
Program terminated (StartMessageLoop was not called).

Release result:
(ArrayList) [0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 50]
10
(ArrayList) [0, 1, 2, 3, 4, 5, 9, 17, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 50]
1
 
Last edited:
Upvote 0

WebQuest

Active Member
Licensed User
How do you get the same result with Strings? And how do I copy for example b4xs elements into a List as list. I need to cast the elements to be able to save them in a SqlLite table.
 
Upvote 0

WebQuest

Active Member
Licensed User
I get this exception when using a regular list containing string elements.

B4X:
java.lang.NumberFormatException: For input string: "Hello"
 
Upvote 0

WebQuest

Active Member
Licensed User
B4X:
Sub Process_Globals
dim List as List
'This list contains string elements loaded from a SqlLite database
End Sub


Sub TrimDuplicaRecordClienti
    list.Initialize
   
    Dim b4xs As B4XSet = B4XCollections.CreateSet
    For Each objItem As Int In list
        b4xs.Add(objItem)
        list.Add(objItem)'NotWork'
    Next   

End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
try
B4X:
For Each objItem As Object In list
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
How do you get the same result with Strings? And how do I copy for example b4xs elements into a List as list
Here is a complete example using strings instead of Int:
B4X:
Dim firstlist As List = Array As String("mark", "hello", "klaus", "jose", "4", "WebQuest", "erel", "WebQuest","klaus")
    Dim b4xs As B4XSet = B4XCollections.CreateSet
    For Each objItem As String In firstlist
        b4xs.Add(objItem)
    Next    
    Log(b4xs.AsList)
    For Each Value As String In b4xs.AsList
        Log(Value)
    Next
'Displays:
(ArrayList) [mark, hello, klaus, jose, 4, WebQuest, erel]
mark
hello
klaus
jose
4
WebQuest
erel
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…