how to Find and count duplicate numbers in a string array in vb.net?

后端 未结 4 2117
旧时难觅i
旧时难觅i 2020-12-12 08:32

how to count the duplicate numbers exist in a string or integer array in vb.net?

Dim a as string = \"3,2,3\"

from the above

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 08:59

    Another option using a Dictionary:

        Dim a As String = "3,2,3"
    
        Dim counts As New Dictionary(Of String, Integer)
        For Each value As String In a.Split(",")
            If Not counts.ContainsKey(value) Then
                counts.Add(value, 1)
            Else
                counts.Item(value) = counts.Item(value) + 1
            End If
        Next
    
        For Each kvp As KeyValuePair(Of String, Integer) In counts
            Debug.Print("Value: " & kvp.Key & ", Count: " & kvp.Value)
        Next
    

    Output:

    Value: 3, Count: 2
    Value: 2, Count: 1
    

提交回复
热议问题