Android Question How to remove duplicate value in array?

JhoeyBoy

Member
Licensed User
Longtime User
This code will remove duplicate value in array using vb.net, how to do it in b4a?



B4X:
Public Function RemoveDuplicates(ByVal items As String()) As String()

    Dim noDupsArrList As New ArrayList()

    For i As Integer = 0 To items.Length - 1

        If Not noDupsArrList.Contains(items(i).Trim()) Then

            noDupsArrList.Add(items(i).Trim())

        End If

    Next


    Dim uniqueItems As String() = New String(noDupsArrList.Count - 1) {}

    noDupsArrList.CopyTo(uniqueItems)

    Return uniqueItems

End Function
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why are you using an array? There is almost never a good reason to use an array.

Why not use B4XSet (B4XCollections)?

B4X:
Dim s As B4XSet = B4XCollections.CreateSet
s.Add("a")
s.Add("a")
s.Add("a")
s.Add("a")
s.Add("bbb")
s.Add("bbb")
s.Add("bbb")
For Each i As String In s.AsList
    Log(i)
Next
 
Upvote 0
Top