none
Visual Basic how to remove duplicate values in an array RRS feed

Answers

  • Hi jbrotto, 

    Thank you for posting here.

    If type of array is class, you need to set the standard of ‘duplicate’ between two classes.

    I make a test on my side which implement the IEquatable  generic interface in the class.

    Here’s the code and you can refer to it:

        Sub Main()
            Dim stuLst As Student() = {New Student() With {
                .Id = 1,
                .Name = "A",
                .Age = 10
            }, New Student() With {
                .Id = 2,
                .Name = "B",
                .Age = 12
            }, New Student() With {
                .Id = 3,
                .Name = "C",
                .Age = 13
            }, New Student() With {
                .Id = 1,
                .Name = "A",
                .Age = 11
            }}
            Dim results = stuLst.Distinct().ToList()
    
            For Each result In results
                Console.WriteLine(result.Id.ToString() + " " + result.Name.ToString() + " " + result.Age.ToString())
            Next
            Console.ReadLine()
        End Sub
    
    

    Student class:

    Public Class Student
        Implements IEquatable(Of Student)
    
        Public Property Id As Integer
        Public Property Name As String
        Public Property Age As Integer
        ''' <summary>
        ''' Standard: If one class's Id and Name is the same as antoher class, then they are duplicate.
        ''' </summary>
        Public Function IEquatable_Equals(ByVal other As Student) As Boolean Implements IEquatable(Of Student).Equals
            If Object.ReferenceEquals(other, Nothing) Then Return False
            If Object.ReferenceEquals(Me, other) Then Return True
            Return Id.Equals(other.Id) AndAlso Name.Equals(other.Name)
        End Function
    
        Public Overrides Function GetHashCode() As Integer
            Return Id.GetHashCode() Xor Name.GetHashCode()
        End Function
    End Class

    Result of the test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by jbrotto Wednesday, January 22, 2020 1:08 AM
    Tuesday, January 14, 2020 7:13 AM
    Moderator

All replies

  • Hi,
    you can use Distinct in LinQ like in this demo:

    Module Module1
      Sub Main()
        Try
          Call (New Demo).Execute()
        Catch ex As Exception
          Dim ex1 = ex
          While ex1 IsNot Nothing
            Console.WriteLine(ex1.Message)
            ex1 = ex1.InnerException
          End While
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
    
      Friend Class Demo
    
        Friend Sub Execute()
          Dim arrOld = GetArray()
          Dim cntOld = arrOld.Count
          Console.WriteLine(cntOld)
          '
          Dim arrNew = GetTrancatedArray(arrOld)
          Dim cntNew = arrNew.Count
          Console.WriteLine(cntNew)
    
        End Sub
    
        Private Function GetArray() As Integer()
          Dim rnd As New Random
          Dim l As New List(Of Integer)
          For i = 1 To 1000
            l.Add(rnd.Next(1, 100))
          Next
          Return l.ToArray
        End Function
    
        Private Function GetTrancatedArray(arr() As Integer) As Integer()
          Return (From item In arr Distinct).ToArray
        End Function
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Sunday, January 12, 2020 6:41 AM
  • Hello,

    Here are two examples, one for strings which works case insensitive and the other againsts integer.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim array1() As Integer = {1, 2, 3, 3, 4, 1}
        Dim array2() As Integer = array1.Distinct().ToArray()
    
        For Each item As Integer In array2
            Console.WriteLine(item)
        Next
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim array1() As String = {"Cat", "Dog", "Bird", "CAT"}
        Dim array2() = array1.Distinct(StringComparer.OrdinalIgnoreCase).ToArray()
    
        For Each item As String In array2
            Console.WriteLine(item)
        Next
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, January 12, 2020 10:42 AM
    Moderator
  • Will this work with values already entered in an array? Also this could be done in a form application?
    Sunday, January 12, 2020 3:23 PM
  • Hi,
    yes it will.

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Monday, January 13, 2020 6:05 AM
  • Hi jbrotto, 

    Thank you for posting here.

    If type of array is class, you need to set the standard of ‘duplicate’ between two classes.

    I make a test on my side which implement the IEquatable  generic interface in the class.

    Here’s the code and you can refer to it:

        Sub Main()
            Dim stuLst As Student() = {New Student() With {
                .Id = 1,
                .Name = "A",
                .Age = 10
            }, New Student() With {
                .Id = 2,
                .Name = "B",
                .Age = 12
            }, New Student() With {
                .Id = 3,
                .Name = "C",
                .Age = 13
            }, New Student() With {
                .Id = 1,
                .Name = "A",
                .Age = 11
            }}
            Dim results = stuLst.Distinct().ToList()
    
            For Each result In results
                Console.WriteLine(result.Id.ToString() + " " + result.Name.ToString() + " " + result.Age.ToString())
            Next
            Console.ReadLine()
        End Sub
    
    

    Student class:

    Public Class Student
        Implements IEquatable(Of Student)
    
        Public Property Id As Integer
        Public Property Name As String
        Public Property Age As Integer
        ''' <summary>
        ''' Standard: If one class's Id and Name is the same as antoher class, then they are duplicate.
        ''' </summary>
        Public Function IEquatable_Equals(ByVal other As Student) As Boolean Implements IEquatable(Of Student).Equals
            If Object.ReferenceEquals(other, Nothing) Then Return False
            If Object.ReferenceEquals(Me, other) Then Return True
            Return Id.Equals(other.Id) AndAlso Name.Equals(other.Name)
        End Function
    
        Public Overrides Function GetHashCode() As Integer
            Return Id.GetHashCode() Xor Name.GetHashCode()
        End Function
    End Class

    Result of the test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by jbrotto Wednesday, January 22, 2020 1:08 AM
    Tuesday, January 14, 2020 7:13 AM
    Moderator