如何在 vb.net 中的字符串数组中查找和计算重复数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30547427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:09:32  来源:igfitidea点击:

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

arraysvb.net

提问by nasr18

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

如何计算vb.net中字符串或整数数组中存在的重复数字?

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

from the above "a" variable i want a count of "3" as 2 (i mean 3 exist 2 times) and "2" as "1". So how do i make it in vb.net?????

从上面的“a”变量中,我想要将“3”计数为 2(我的意思是 3 存在 2 次),将“2”计数为“1”。那么我如何在 vb.net 中制作它????

Actually i will get the above string "a" from the sql database. so i dont know which numbers are there. That's why i am asking here.

实际上,我将从 sql 数据库中获取上述字符串“a”。所以我不知道那里有哪些数字。这就是我在这里问的原因。

回答by Shar1er80

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 IEnumerableobject of all unique items in the array
  • .ToList()- Turns the IEnumerableobject into a List<T>
  • .ForEach()- Iterates through the List<T>
    • Sub(digit)- Defines an Actiondelegate 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 Funcdelegate that will count each occurrence of digits in the array. Each element, in str(), during the Count iterations are stored in the variable sand if it matches the digit variable from Sub(digit)it will be counted
  • str.Distinct()- 返回IEnumerable数组中所有唯一项的对象
  • .ToList()- 将IEnumerable对象变成一个List<T>
  • .ForEach()- 遍历List<T>
    • Sub(digit)- 定义要在每个元素上执行的Action委托。在每次迭代期间,每个元素都被命名为 digit。
    • 你应该知道Console.WriteLine()在做什么
    • str.Count()- 将每次出现计数满足条件的数字
      • Function(s) s = digit- 定义一个Func委托,该委托将对数组中数字的每次出现进行计数。str()在 Count 迭代过程中,每个元素 in都存储在变量中s,如果它与来自Sub(digit)它的数字变量匹配,则将对其进行计数

Results:

结果:

1 exists 4
2 exists 2
3 exists 1
0 exists 1
4 exists 1

回答by Sehnsucht

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 :

如果你有一个像你的例子中的字符串,首先根据你的分隔符分割它;那么您可以使用 GroupBy Linq 查询:

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 }

回答by Idle_Mind

Another option using a Dictionary:

使用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

回答by Keith Beard

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

我想,这听起来有点像家庭作业