以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  请教集合  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=129594)

--  作者:hbhb
--  发布时间:2019/1/2 12:31:00
--  请教集合
大师:请问求集合或数组中的字符重复次数最多的那个值代码?


--  作者:有点甜
--  发布时间:2019/1/2 12:48:00
--  

弄一个字典,存放元素以及其个数。

 

循环每一个集合,计算得到值,最后循环字典,读取元素和个数。


--  作者:hbhb
--  发布时间:2019/1/2 12:59:00
--  
我要直接得到那个值,劳驾写一个函数给我!


--  作者:有点甜
--  发布时间:2019/1/2 15:22:00
--  
Dim ary() = {1,2,3,1,1,1,2,3,1,1}
Dim dic As new Dictionary(of String, Integer)
Dim str As String = ""
Dim max As Integer = 0
For Each s As String In ary
    If dic.ContainsKey(s) = False Then
        dic.add(s, 1)
    Else
        dic(s) += 1
    End If
    If dic(s) > max Then
        str = s
        max = dic(s)
    End If
Next
msgbox(str & " " & max)

--  作者:y2287958
--  发布时间:2019/1/2 16:26:00
--  
如果存在2个甚至更多个同样的最大值呢?
--  作者:有点甜
--  发布时间:2019/1/2 16:33:00
--  
以下是引用y2287958在2019/1/2 16:26:00的发言:
如果存在2个甚至更多个同样的最大值呢?

 

那你就循环dic字典后,得到你需要的值。