bharath kumar
bharath kumar

Reputation: 165

maximum occurance of an element in an array in tcl

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

Answers (1)

glenn jackman
glenn jackman

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

Related Questions