如何计算vb.net中字符串或整数数组中存在的重复数字?
Dim a as string = "3,2,3"
从上面的“a”变量我希望计数“3”为2(我的意思是3存在2次)和“2”为“1”。那么我如何在vb.net中制作它?????
实际上我将从sql数据库中获取上面的字符串“a”。所以我不知道那里有哪些数字。这就是我在这里问的原因。
答案 0 :(得分:1)
如果您的示例中有一个字符串,请根据您的分隔符将其拆分;然后你可以使用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 }
答案 1 :(得分:1)
使用Dictionary的另一个选项:
java.io.File
输出:
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
答案 2 :(得分:1)
你已经有了一些很好的答案可供选择,但我认为你会对单线解决方案感兴趣。
$scope.svg = $sce.trustAsHtml('<path fill="#F5F5F5" d="M153.065,320.752l-43.868,14.321c28.69,86.341,107.159,149.954,201.385,156.805l3.875-42.49 C238.671,442.564,175.884,390.696,153.065,320.752z"></path> <path fill="#E0E0E0" d="M314.457,449.388l4.208-46.141c-57.075-5.139-104.36-44.202-121.546-96.877l-44.054,14.382 C175.884,390.696,238.671,442.564,314.457,449.388z"></path> <path fill="#CCCCCC" d="M241.173,291.988l-44.054,14.382c17.185,52.675,64.471,91.738,121.546,96.877l4.208-46.141 C284.509,353.651,252.725,327.395,241.173,291.988z"></path> <path fill="none" stroke="#000000" stroke-miterlimit="10" d="M244.694,277.988l-50.225,16.397l-50.225,16.397l-50.013,16.327 c32.708,98.435,122.169,170.959,229.594,178.77l4.418-48.442l4.797-52.604l4.797-52.604 C294.1,348.289,257.864,318.354,244.694,277.988z"></path> <path d="M275.825,310.202c0.801,0.493,1.971,0.904,3.204,0.904c1.828,0,2.895-0.965,2.895-2.362c0-1.293-0.739-2.033-2.608-2.752 c-2.259-0.801-3.656-1.971-3.656-3.922c0-2.156,1.787-3.758,4.477-3.758c1.417,0,2.444,0.329,3.06,0.677l-0.493,1.458 c-0.451-0.247-1.375-0.657-2.629-0.657c-1.889,0-2.608,1.13-2.608,2.074c0,1.293,0.842,1.931,2.752,2.67 c2.342,0.904,3.532,2.033,3.532,4.067c0,2.135-1.581,3.984-4.847,3.984c-1.334,0-2.792-0.39-3.532-0.884L275.825,310.202z"></path> <path d="M293.347,312.009c-0.472,0.247-1.519,0.575-2.854,0.575c-2.998,0-4.95-2.033-4.95-5.072c0-3.06,2.095-5.278,5.34-5.278 c1.068,0,2.012,0.267,2.505,0.513l-0.411,1.396c-0.431-0.246-1.109-0.472-2.094-0.472c-2.28,0-3.512,1.685-3.512,3.758 c0,2.3,1.479,3.717,3.451,3.717c1.027,0,1.705-0.267,2.218-0.492L293.347,312.009z"></path> <path d="M296.351,307.738c0.041,2.444,1.602,3.45,3.409,3.45c1.293,0,2.074-0.226,2.752-0.513l0.309,1.294 c-0.637,0.288-1.725,0.616-3.307,0.616c-3.06,0-4.888-2.012-4.888-5.011c0-2.998,1.767-5.359,4.662-5.359 c3.245,0,4.108,2.854,4.108,4.682c0,0.37-0.042,0.657-0.062,0.842H296.351z M301.649,306.444c0.02-1.15-0.472-2.936-2.506-2.936 c-1.828,0-2.629,1.684-2.772,2.936H301.649z"></path> <path d="M305.638,305.129c0-1.027-0.02-1.869-0.082-2.69h1.602l0.102,1.643h0.042c0.492-0.945,1.643-1.869,3.286-1.869 c1.375,0,3.512,0.821,3.512,4.23v5.935h-1.808v-5.73c0-1.602-0.595-2.936-2.3-2.936c-1.191,0-2.115,0.842-2.423,1.848 c-0.082,0.226-0.123,0.534-0.123,0.842v5.976h-1.808V305.129z"></path> <path d="M322.546,312.379l-0.144-1.252h-0.062c-0.554,0.78-1.622,1.478-3.039,1.478c-2.012,0-3.039-1.417-3.039-2.854 c0-2.403,2.135-3.717,5.975-3.696v-0.206c0-0.821-0.226-2.3-2.259-2.3c-0.924,0-1.889,0.288-2.587,0.739l-0.411-1.191 c0.822-0.534,2.013-0.883,3.266-0.883c3.039,0,3.778,2.073,3.778,4.066v3.717c0,0.862,0.041,1.704,0.165,2.382H322.546z M322.28,307.307c-1.971-0.041-4.21,0.308-4.21,2.238c0,1.171,0.781,1.726,1.705,1.726c1.293,0,2.115-0.822,2.403-1.664 c0.061-0.185,0.103-0.39,0.103-0.575V307.307z"></path>');
解释发生了什么:
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
Distinct() - 返回数组中所有唯一项的str.
对象IEnumerable
ToList() - 将.
对象变为IEnumerable
List<T>
ForEach() - 通过.
进行迭代
结果:
Sub(digit)
答案 3 :(得分:0)
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)
我猜,听起来有点像家庭作业