# 一番出力された文字は何??
# input this is a pen. this is an apple. applepen!
# output ('p',6)
def count_check(str)
#1.メソッドを探す
#2.配列のループからハッシュのキーとバリューに保存していく。最終的にkey、valueを出力する。
str_list = str.split('')
str_list.delete(" ")
result = Hash.new
str_list.each do |str|
#もうすでにハッシュの中にあるのかを確かめる
if result.key?(str)
print str
# result[:str]
result[str] += 1
else
result.store(str,1)
end
end
# #キーが最大の文字列を探す。
max = 0
ans = []
result.each{|key,value|
if value > max
ans.pop(2)
ans.push(key,value)
max = value
end
}
return ans
end
print(count_check("this is a pen. this is an apple. applepen!"))
More than 3 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme