# Thread: View the most occurring value from an arraylist

1. Member
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.

2. 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?

3. Member
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();

}

}

}```
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.

4. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Put the values in a frequency Map<Integer, Temp>. So you loop through the arraylist once setting the object frequencies in the map.

5. Member
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.

6. Member
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);

}
}
}
}```
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.

7. 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.

8. Member
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?

9. 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());```

10. Member
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.

11. Did you read the API?

Using simple logic you can do that, loop through each item and check for the maximum value at least.

12. Member
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..

13. Originally Posted by frozensun
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..
You should learn the basis first. I just give you an example, if you don't know how to change accordingly at least, better to keep this for min away and study about the basis.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•