Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
sample input:
7
1
2
3
2
2
3
4
sample output:
2

1) 1st line of input is the total number of inputs 'n'.

What I have tried:

Ruby
number = gets.champ.to_
for i in 1..number
    num = gets.champ-to_i
Posted
Updated 26-Sep-22 20:46pm

This
Ruby
puts "how many items?"
items = gets.to_i
arr = Array.new(items,10)
for i in 0..items-1
  puts "please enter item #{i}"
  arr[i] = gets.to_i
end
should fix your code. Now it is up to you actually fulfilling the requirements.
 
Share this answer
 
Once you have fixed your code as recommended by CPallini, it's pretty easy to handle the second part, if you think about it.

You need the most repeating value, so you need to count all of the identical values - which is complicated with random order data.
But ... it you sort your data before you start counting, all the identical values will be next to each other, so counting them and working out the moist frequent becomes pretty much trivial: try it on paper with your sample data and you will see what I mean.
7, 1, 2, 3, 2, 2, 3, 4
Sorted becomes
1, 2, 2, 2, 3, 3, 4, 7
and it's easy to see that there are more '2's than any other value in a single pass though the collection - and this works regardless of how many items the collection contains.

Hint: Ruby has a sort function or two: How to Sort Arrays & Hashes in Ruby (Examples Included)[^]

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900