Consider an object list of Item where each item is associated with an integer field:
Item[0]->1
Item[1]->4
Item[2]->2
Item[3]->9
Item[4]->1
Item[5]->9
Item[6]->3
Item[7]->6
Item[8]->7
Item[9]->9
I want to filter out a list with the items that have the maximum value. In that case since the maximum number is 9, I will receive {Item[3],Item[5],Item[9]}
. My way of doing this is having to iterate the whole list first, then store the maximum value (9) somewhere and then iterate it again and add the items which have their field equal to 9 to a new list.
But this is a lot of code everytime I want to do something similar, and doesn't seem very efficient. Is there a better approach (either in terms of efficiency or tidyness)?