Mr. Black
Mr. Black

Reputation: 12082

Count character is consecutively repeated the most times in the given string

Anybody know, how to count the character is consecutively repeated the most times in the given string. I need very shortest answer.

For ex: "xxyyydduuummm" will return 'y, u, m'

Upvotes: 0

Views: 874

Answers (1)

sawa
sawa

Reputation: 168101

"xxyyydduuummm".scan(/((.)\2*)/).group_by{|s, c| s.length}.sort_by(&:first).last.last.map(&:last) (Ruby 1.9)
"xxyyydduuummm".scan(/((.)\2*)/).group_by{|s, c| s.length}.sort_by{|k, v| k}.last.last.map{|s, c| c} (Ruby 1.8.7)
# => ["y", "u", "m"]

Improvement Suggested by Mladen Jablanović.

"xxyyydduuummm".scan(/((.)\2*)/).group_by{|s, c| s.length}.max.last.map(&:last) (Ruby 1.9)
"xxyyydduuummm".scan(/((.)\2*)/).group_by{|s, c| s.length}.max.last.map{|s, c| c} (Ruby 1.8.7)
# => ["y", "u", "m"]

Upvotes: 6

Related Questions