Q:

Write a Ruby program to find most occurred item in a given array

0

Write a Ruby program to find most occurred item in a given array. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

nums = [10, 20, 30, 40, 10, 10, 20]
print "Original array:\n"
print nums
nums_freq = nums.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
print "\nFrequency of numbers:\n"
print nums_freq

Output:

Original array:
[10, 20, 30, 40, 10, 10, 20]
Frequency of numbers:
{10=>3, 20=>2, 30=>1, 40=>1}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now