How to count the numbers in array by their first N digits in Ruby

1 Answer

0 votes
arr = [589, 8347, 5893725, 2, 1817112, 58900123]

result = arr.count { |num| num.to_s.start_with? '589' }

puts result



 
 
#
# run:
# 
# 3
#

 



answered Oct 17, 2021 by avibootz
...