I know how to find the largest and smallest elements of a Java:
- sort the array
- use a for loop to iterate over the array and check for the smallest.
But is there a way to check for the largest or smallest in one statement? Also, order needs to be preserved.
Conditions:
- No method calls in the same class
- Sequence is unsorted, and remains unsorted
- No access to external libraries (i.e.
ArrayUtils
is not allowed).
Assume that this is a plain Java array, i.e. int[]
, not ArrayList<T>
or LinkedList<T>
.