finding the mode of a 'double' array
Author |
Message |
hamham3001
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: 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) |
|
|
|
|
|
![](images/spacer.gif) |
|
|