Answers for "how to count number of character in a string"

0

count characters in string

# count characters iterative
def count_chars(s)
  count = Hash.new(0)
  s.split("").each do |x|
    count[x] += 1
  end
  count
end

# count characters functional
s.chars.map {|c| [c, s.chars.count(c)] }.to_h
Posted by: Guest on May-21-2021

Code answers related to "how to count number of character in a string"

Code answers related to "Javascript"

Browse Popular Code Answers by Language