Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Me!  RSS feed

 
Kwesi Prat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problem trying to figure out the code that will return the mode(the most appearing number in a list of numbers). Could somebody come to my aid?
This is my code so far. Thanks.
public int getMode() {
for (int i = 0; mArray.lengh; i++) {
}
return mode;
}
 
Angel Dobbs-Sciortino
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Off the top of my head, I would use a HashMap. Something like this, assuming mArray is an int[]:

Angel
[ December 03, 2003: Message edited by: Angel Dobbs-Sciortino ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a voting algorithm that finds the winner in a single pass through the data: http://www.cs.utexas.edu/users/moore/best-ideas/mjrty/index.html I don't pretend to understand it, but it looks pretty cool. I'm not certain it gives a good plurality - winner with less than 50%. See what Moore has to say. Read the other links on the "good ideas" link for more fun.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!