Write a Ruby program to find most occurred item in a given array.
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}
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
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