We are required in our assignment to find the second smallest integer in one array recursively. However, for the sake of understanding the subject more, I want to do it iter
How about this?
int[] result = Arrays.asList(-3, 4,-1,-2).stream()
.reduce(new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE},
(maxValues, x) -> {
if (x > maxValues[0]) {
maxValues[1] = maxValues[0]; //max becomes second max
maxValues[0] = x;
}
else if (x > maxValues[1]) maxValues[1] = x;
return maxValues;
}
, (x, y) -> x);