This content has been marked as final. Show 1 reply
Most of these biggest/smallest/greenest/etc problems are got at the same way:
(1) Start with a sensible value for the "result". Often the first element of the list.
(2) Write a loop that goes through the list. Compare the list item with the result to see if it is bigger/smaller/greener/etc. If it is it can replace the result.
(3) Once the loop finishes the result represents the biggest/smallest/greenest/etc element in the list.
Your version of the problem adds a couple of small details to be resolved: First, as well as the "result" (the biggest/smallest/greenest/etc so far) you have to remember its index in the list. And, secondly, the first element in the list may not be what I described in (1) as a suitable starting value for "result" - indeed you must be prepared to cope with the case where there is no valid index to return from getIndexOfClosestEntry().