Win a copy of Design for the Mind this week in the Design forum!

# Getting the most frequent value.

Vincent Oh
Ranch Hand
Posts: 33
Let's say i got a few double[2] : {a, b}

{3.0, 3.0}, {1.0, 1.0}, {2.0, 2.0}, {3.0, 2.0}

How do I sort them based on "a" so that I can retrieve the most frequent "a"?

I tried the following:

which prints

But how do i retrieve the value 3.0 which is "a"?

Thanks.

Jayesh A Lalwani
Rancher
Posts: 2756
32
So, you know how to get the frequency of each value of "a" right? Try to do this on paper, and go step by step

You got the first "a" in the list let's say A1, and you got the Frequncy of A1, let's call it F1. Now, let's say you get the second "a" in the list, let's call it A2, and you get the frequency of A2, let's call it F2. How will you know between A1 and A2, which one is more frequent? Once you did that, how will you do the same thing with A1, A2 and A3. How will you do that with A1, A2, A3... An.

Vincent Oh
Ranch Hand
Posts: 33
It's ok, i got it using SortedMap map = new TreeMap;

Cheers.