• Post Reply Bookmark Topic Watch Topic
  • New Topic

Find single or multiple mode in array  RSS feed

 
kesh tom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to find the mode in a given sorted list. By mode, I mean the number that is repeated most. Managed to work out the following code but it will not work if there are more than 1 mode in an array.
For example: [1,2,3,3] This array's mode is the number 3 as in more frequent.
But in this example: [1,2,2,3,3] both 2 and 3 are both modes cos they repeat the same number of times. My code will only recognize and return the number 2 which is wrong. I do get the idea that I should be returning an array instead of an int but can't quite get the picture how to store the information in an array correctly.

Please advice. Thank you.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Go through it with a pencil and paper and work out how you would do that by hand. Then convert that to what the computer can use. Then you can create the code.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!