Remove duplicates from array in VB.net

Remove duplicates from array in VB.net

Here’s a function that will remove duplicate items in a array. The code is for VB.net.

The code:

    Function RemoveDuplicates(ByVal initialArray As String())
        Dim i As Integer
        Dim j As Integer
        Dim newArray(0) As String

        For i = 0 To UBound(initialArray)
            For j = 0 To UBound(initialArray)
                If Not initialArray(i) = "" Then
                    If Not j = i Then
                        If initialArray(i) = initialArray(j) Then
                            initialArray(j) = ""
                        End If
                    End If
                End If
            Next
        Next

        j = 0
        For i = 0 To UBound(initialArray)
            If Not initialArray(i) = "" Then
                ReDim Preserve newArray(j)
                newArray(j) = initialArray(i)
                j += 1
            End If
        Next

        Return newArray

    End Function

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.