# View the most occurring value from an arraylist

• 10-31-2009, 06:33 PM
frozensun
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.
• 10-31-2009, 06:40 PM
Fubarable
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?
• 11-01-2009, 02:25 AM
frozensun
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:

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();                         }                 }              }```
But now I have to get the value what is most occuring in the array. To make it clearlier, I want to have the Mode(statistics)(see wikipedia). I hope someone can help me further.
• 11-01-2009, 07:13 AM
r035198x
Put the values in a frequency Map<Integer, Temp>. So you loop through the arraylist once setting the object frequencies in the map.
• 11-01-2009, 01:43 PM
frozensun
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.
• 11-01-2009, 02:07 PM
frozensun
I have now the following, by myself:

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);                                   }         }         }     }```
I checked with System.out.println(b); that I get more values with the same content. But now I need to count how much values there are with the same content. And return that.
• 11-01-2009, 05:07 PM
Eranga
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.
• 11-01-2009, 05:17 PM
frozensun
Ok, that's clearly how it works, thanks.
But how do I get the value's into a Map, and read them out?

• 11-01-2009, 05:22 PM
Eranga
Here is a very simple code segment for your reference. Carefully go through it.

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());```
• 11-01-2009, 05:49 PM
frozensun
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.
• 11-01-2009, 05:59 PM
Eranga