Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 finding the mode of a 'double' array
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hamham3001




PostPosted: Sun Apr 24, 2005 7:49 pm   Post subject: finding the mode of a 'double' array

um.. i have an assignment where i have to find stuff from a user input array.. one question asked to find the most frequent number in the 'double' array. im not sure how to do this.. can anyone help? thanks alot
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Apr 24, 2005 10:35 pm   Post subject: (No subject)

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Hashtable.html

With a hashtable you can have each number in the array as a key. Then, at that key you store the number of times that number occurs. To find the most common number simply look for the key with the largest count attached to it.

A simple example in Ruby, since this sounds like a homework assigment, and I don't want to just give you code.

code:
arr = [98.3, 34.6, 78.9, 34.6, 12.4, 89.4, 34.6, 98.3]
hash = {}

for num in arr
   if hash.has_key? num
      hash[num] += 1
   else
      hash[num] = 1
   end
end

most_common = hash.index(hash.values.sort.last)
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: