Reputation: 165
I have an array as follows
array = { a a a a b b b c c c c c c}
in the above array "a" is accured 4 times , b is 3 times , c as 6 times
could you help me with the proc to get the output as 6 if my input is above array
Thanks
Upvotes: 1
Views: 426
Reputation: 247022
Using a dictionary to hold the count of each element, then the max
function to get the maximum value.
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
The "splat" ({*}
) expands a list of values into its individual elements to feed into the max
function.
Upvotes: 6