gpt4 book ai didi

asp.net - 列表中的重复项

转载 作者:行者123 更新时间:2023-12-02 16:56:07 25 4
gpt4 key购买 nike

我在 VB.Net 中遇到这个问题。

我有一个字符串类型的列表。

Dim list As New List(Of String)

此列表可能包含也可能不包含重复项。
现在我想要的是,假设列表有值 {"10", "10", "10", "11", "11", "12"}
我想创建一个数组(二维)/列表,它将给我这样的值。
(3,10;(2,11);(1,12)

简单表示 10 出现 3 次,11 出现 2 次,12 出现 1 次。
请不要给我任何 LINQ 回复,因为我正在使用 VB.Net 2.0

最佳答案

在 .NET 2 中,您必须自己跟踪这一点。最简单的方法可能是构建您自己的 Dictionary(Of String, Integer) 来存储计数,并手动循环:

Dim dict = New Dictionary(Of String, Integer)
For Each value in list
If dict.ContainsKey(value) Then
Dim count = dict(value)
dict(value) = count + 1
Else
dict(value) = 1
End If
Next

' dict now contains item/count
For Each kvp in dict
Console.WriteLine("Item {0} has {1} elements", kvp.Key, kvp.Value)
Next

关于asp.net - 列表中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18064074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com