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

后端 未结 4 2107
旧时难觅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条回答
  • 2020-12-12 08:50
    Dim count2 as integer = 0
    Dim count3 as integer = 0    
    For Each c As Char in a
     if c = "3"
          count3 += 1
     else if c = "2"
          count2 += 1
     end if
    Next
    console.writeline(count2)
    console.writeline(count3)
    

    I guess, it kinda sounds like homework

    0 讨论(0)
  • 2020-12-12 08:54

    You've already got some good answers to choose from, but I thought you'd be interested in a one liner solution.

    Module Module1
        Sub Main()
            Dim str() As String = "1,2,1,2,3,1,0,1,4".Split(","c)
            str.Distinct().ToList().ForEach(Sub(digit) Console.WriteLine("{0} exists {1}", digit, str.Count(Function(s) s = digit)))
            Console.ReadLine()
        End Sub
    End Module
    

    Explanation as to what's happening:

    • str.Distinct() - Returns an IEnumerable object of all unique items in the array
    • .ToList() - Turns the IEnumerable object into a List<T>
    • .ForEach() - Iterates through the List<T>
      • Sub(digit) - Defines an Action delegate to perform on each element. Each element is named digit during each iteration.
      • You should know what Console.WriteLine() is doing
      • str.Count() - Will count each occurrence a digit that satisfies a condition
        • Function(s) s = digit - Defines a Func delegate that will count each occurrence of digits in the array. Each element, in str(), during the Count iterations are stored in the variable s and if it matches the digit variable from Sub(digit) it will be counted

    Results:

    1 exists 4
    2 exists 2
    3 exists 1
    0 exists 1
    4 exists 1
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-12 09:07

    If you have a string like in your example start by splitting it according to your delimiter ; then you can use a GroupBy Linq query :

    Dim source = "3,2,3".Split(","c)
    
    Dim query = From item In source
                Group By item Into Count()
    
    For Each result In query
        Console.WriteLine (result)
    Next
    
    ' output
    ' { item = 3, Count = 2 }
    ' { item = 2, Count = 1 }
    
    0 讨论(0)
提交回复
热议问题