Is there any simpler way to find all elements in a list that are equal to the max element.
List v = [ 1,2,3,4,5,5 ]
def max = v.max()
def maxs = v.findAll { it == max }
Thanks!
|
How you've done it for the simple example is exactly how I would do it. I may use
|
|||
|
|
|
In your code you go two times over your list (one time for the method When your list just contains primitives, it is enough to store the maximum and the count of this maximum in one variable, to go through the list and update both variables:
Of course the above code has more lines than yours, but should be faster for very large lists. |
|||
|
|
|
I think this way is the most convenient one:
|
|||
|
|
v.findAll { it == v.max() }– Arturo Herrero Feb 24 '12 at 17:41