Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby get the number of same element in array

names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
counts = Hash.new(0)
names.each { |name| counts[name] += 1 }
# => {"Jason" => 2, "Teresa" => 1, ....
Comment

ruby get the number of same element in array

names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
counts = names.each_with_object(Hash.new(0)){ |string, hash| hash[string] += 1 }
puts counts # => { Jason: 2, Teresa: 1, Judah: 3, ... }
Comment

PREVIOUS NEXT
Code Example
Ruby :: rails hidden field default value 
Ruby :: ruby median find 
Ruby :: date time string to time in rails 
Ruby :: how to make a new array ruby 
Ruby :: ruby reverse string 
Ruby :: add edit or --wait for rails credentials edit windows 
Ruby :: new line in ruby 
Ruby :: times ruby 
Ruby :: save to csv ruby 
Ruby :: how to update a field on after_save rails 
Ruby :: ruby get current pid 
Ruby :: ruby pry syntax 
Ruby :: rails model naming convention 
Ruby :: ruby while loop 
Ruby :: helper path outside view 
Ruby :: format date rails created long 
Ruby :: rails not_found 
Ruby :: ruby on rails array contains multiple values 
Ruby :: rails destroy something from db 
Ruby :: rails edit models 
Ruby :: SoC partial class 
Ruby :: rails model on validation custom column name 
Ruby :: rails add index from console 
Ruby :: rails add element to array 
Ruby :: how to use action_view in console rails 
Ruby :: ruby decode base64 
R :: convert latin accents to ascii R 
R :: r extract everything before character 
R :: merge multiple data frames in r 
R :: select columns in r 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =