Count the number of occurrences of a string in Ruby

This article uses Ruby 2.6.3 installed on macOS.

I made a note of how to solve the Ruby super-introduction exercise (p154) that can be understood from scratch. I realized that I need to use hashes well for those that have a one-to-one correspondence.

Q: Please count the alphabets used in the string "caffe latte" and the number of times.


hash = {}
hash.default = 0

#Arrange each character in an array and sort them in alphabetical order
array = "caffelatte".chars.sort 

#Character key,Create a hash with the number of times as the value
array.each { |x| hash[x] += 1} 

#Shows how many times the character was used at the end
hash.each { |letter, count| puts "#{letter}Is#{count}Used times"} 

Recommended Posts

Count the number of occurrences of a string in Ruby
[Ruby] Count an even number in an array using the even? Method
Determine that the value is a multiple of 〇 in Ruby
How to change a string in an array to a number in Ruby
The nth and n + 1st characters of a Ruby string
Count the number of digits after the decimal point in Java
A program that counts the number of words in a List
Find the number of days in a month with Kotlin
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
Extract a part of a string with Ruby
[Java] Check the number of occurrences of characters
Generate a serial number with Hibernate (JPA) TableGenerator and store it in the Id of String.
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
The story of low-level string comparison in Java
[Delete the first letter of the character string] Ruby
Measure the size of a folder in Java
I checked the number of taxis with Ruby
Create a native extension of Ruby in Rust
Call a method of the parent class by explicitly specifying the name in Ruby
[Ruby] Cut out a string using the slice method
[Ruby] The role of subscripts in learning elements in arrays
Calculate the difference between numbers in a Ruby array
[Ruby / Rails] Set a unique (unique) value in the class
Set the maximum number of characters in UITextField in RxSwift
Get the URL of the HTTP redirect destination in Ruby
Find the greatest common divisor and least common multiple of any number of integers in Ruby
Write a test by implementing the story of Mr. Nabeats in the world with ruby
Count the frequency of occurrence of words in a sentence by stream processing (Apache Apex)
Multiplication in a Ruby array
Judgment of fractions in Ruby
Cut out a Ruby string
About truncation by the number of bytes of String on Android
A quick explanation of the five types of static in Java
Check the dependency of a specific maven artifact in Coursier
Put the file in the properties of string in spring xml configuration
[Ruby] Questions and verification about the number of method arguments
I want to call a method and count the number
A note about the seed function of Ruby on Rails
[Ruby] How to retrieve the contents of a double hash
About the behavior of ruby Hash # ==
Sorting hashes in a Ruby array
Basics of sending Gmail in Ruby
Create a large number of records with one command using the Ruby on Rails seeds.rb file
Pit of count method (Be careful of issuing a large number of queries!)
Implementation of ls command in Ruby
Get a proxy instance of the component itself in Spring Boot
[Ruby] See the essence of ArgumentError
About the method to convert a character string to an integer / decimal number (cast data) in Java
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
[Ruby] Insert, replace, destroy at the end of the character string [b021]
A memorial service for the library used when competing in Ruby
String output method memo in Ruby
Implement a gRPC client in Ruby
A memorandum of the FizzBuzz problem
Various methods of the String class
{The first consecutive 10-digit prime number in the value of e} .com
Get the public URL of a private Flickr file in Java
[Illustration] Finding the sum of coins with a recursive function [Ruby]
Let's create a TODO application in Java 5 Switch the display of TODO
Order of processing in the program
How to check for the contents of a java fixed-length string