Avoids stream multiple times
import java.util.stream.IntStream; import java.util.IntSummaryStatistics; interface HighLow { static int[] findLargestAndSmallest(int[] nums) { if(nums == null || nums.length == 0) return null; IntSummaryStatistics stats = IntStream.of(nums).summaryStatistics(); return new int[]{stats.getMin(), stats.getMax()}; } }
import java.util.Arrays;- import java.util.stream.IntStream;
- import java.util.IntSummaryStatistics;
- interface HighLow {
- static int[] findLargestAndSmallest(int[] nums) {
- if(nums == null || nums.length == 0) return null;
int min = Arrays.stream(nums).min().orElseThrow();int max = Arrays.stream(nums).max().orElseThrow();return new int[]{min, max};- IntSummaryStatistics stats = IntStream.of(nums).summaryStatistics();
- return new int[]{stats.getMin(), stats.getMax()};
- }
- }