Results 1 to 13 of 13
 10312009, 05:33 PM #1Member
 Join Date
 Oct 2009
 Posts
 7
 Rep Power
 0
View the most occurring value from an arraylist
Hello,
I have an arraylist with double values, the most occurring value (so the value that occurs most often in the arraylist) should be returned.
My question is that someone maybe can help me to solve this. I use the program BlueJ.
I hope someone can help me asap.
Thanks.

To get the most out of your course and this forum, you should first attempt to solve this problem yourself, and then if you run into a snag, post your code. So what have you tried?
 11012009, 01:25 AM #3Member
 Join Date
 Oct 2009
 Posts
 7
 Rep Power
 0
I had the method to get all values into an array, but then I don't know how to get further. What I have is definately good:
Java Code:public double getModas() { double[] a = new double[1480]; for (Temp m : wd.getAllBla()) { for(int index = 0; index < a.length; index++){ a[index] = m.getCode(); } } }
 11012009, 06:13 AM #4Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 7
Put the values in a frequency Map<Integer, Temp>. So you loop through the arraylist once setting the object frequencies in the map.
 11012009, 12:43 PM #5Member
 Join Date
 Oct 2009
 Posts
 7
 Rep Power
 0
Sorry, but I don't understand what you mean. Can you perhaps explain how do I do it? Sorry, but I am a bit new to java.
 11012009, 01:07 PM #6Member
 Join Date
 Oct 2009
 Posts
 7
 Rep Power
 0
I have now the following, by myself:
Java Code:public double getModas() { double[] a = new double[1480]; for (Temp m : ws.getAllBla()) { for(int index = 0; index<a.length; index++){ a[index] = m.getCode(); if(a[index] == a[index]){ double b = (a[index]); // System.out.println(b); } } } }
 11012009, 04:07 PM #7
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 20
Okay, think like this. You have an array with large number of elements, and some of them duplicated, as follows.
{1, 2, 1, 4, 5, 1, 2, 5, 6, 8}
You can see there numbers like 1, 2, etc are duplicated. You want to count each occurrence on each number and get the maximum occurrence. So before get the exact solution you need to count each of them and keep, and that result should be like follows.
1 > 3 times
2 > 2 times
4 > 1 time
5 > 2 times
6 > 1 time
8 > 1 time
You have each item and the number of occurrence corresponding to. Map in Java do the same for you.
 11012009, 04:17 PM #8Member
 Join Date
 Oct 2009
 Posts
 7
 Rep Power
 0
Ok, that's clearly how it works, thanks.
But how do I get the value's into a Map, and read them out?
Thanks in advance.
 11012009, 04:22 PM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 20
Here is a very simple code segment for your reference. Carefully go through it.
Java Code:int[] myArray = {1, 2, 1, 4, 5, 1, 2, 5, 6, 8}; TreeMap <Integer, Integer> tMap = new TreeMap<Integer, Integer>(); for(int index = 0; index < myArray.length; index++) { if(tMap.get(myArray[index]) == null) { tMap.put(myArray[index], 1); } else { int val = tMap.get(myArray[index]); tMap.put(myArray[index], ++val); } } System.out.println(tMap.keySet()); System.err.println(tMap.values());
 11012009, 04:49 PM #10Member
 Join Date
 Oct 2009
 Posts
 7
 Rep Power
 0
Thank you!
And how do I now return the most occuring number?(here it is 1)
And my value's in the array are doubles, I don't know of it matters.
 11012009, 04:59 PM #11
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 20
Did you read the API?
Using simple logic you can do that, loop through each item and check for the maximum value at least.
 11012009, 05:06 PM #12Member
 Join Date
 Oct 2009
 Posts
 7
 Rep Power
 0
But you can't put doubles in the Map? It gives an error with tMap.put(a[index], 1); because a[index] is a double..
 11012009, 06:13 PM #13
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 20
Similar Threads

A view to help
By hervey in forum Reviews / AdvertisingReplies: 1Last Post: 10292008, 07:37 AM 
Updating a view using actions in a seperate view
By xcallmejudasx in forum EclipseReplies: 0Last Post: 10242008, 09:24 PM 
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04212008, 11:43 AM
Bookmarks