arraysvb.netduplicatesdistinct-values

get distinct values from array ( as String not List or ArrayList) in visual basic


All of the values of labels are stored into an array when the button is clicked

Dim myArr(3) As String           
myArr(0) = 1.Text  
myArr(1) = 2.Text
myArr(2) = 3.Text  
myArr(3) = 4.Text 

I am trying to remove duplicates from my array of labels, I've watched tutorials about distinct but it shows List and ArrayList. I've also coded for removing duplicates from my array

Dim testDist As String = myArr.Distinct().ToString  
           For Each NOW As String In testDist  
                Dim labelShow As String = String.Join(",", NOW)  
                Label7.Text = labelShow  
            Next  

But it only shows ] as output. Can you please help me.


Solution

  • You were on the right track with String.Join but you should have just passed the result of Distinct directly:

    Label7.Text = String.Join(",", myArr.Distinct())
    

    For the record, Disinct returns an IEnumerable(Of String), i.e. a list that can be enumerated. String.Join will accept basically any enumerable list of Strings or Objects, including arrays or generic Lists. It the items are not Strings, their ToString methods will be called.