tcl 数组中元素的最大出现次数

maximum occurance of an element in an array in tcl

我有一个数组如下

数组={a a a a b b b c c c c c}

上面数组中"a"被计算了4次,b是3次,c是6次

如果我的输入在数组之上,你能帮我处理程序使输出为 6吗

谢谢

使用dictionary to hold the count of each element, then the max函数获取最大值。

set array { a a a a b b b c c c c c c}
foreach elem $array {dict incr count $elem}
set max [tcl::mathfunc::max {*}[dict values $count]]
puts $max   ; # => 6

"splat" ({*}) 将值列表扩展到其各个元素中以馈入 max 函数。