Skip to content

Instantly share code, notes, and snippets.

@sendelufa
Created December 18, 2023 07:15
Show Gist options
  • Save sendelufa/5c50e55056b5b85f5e23a78314d237f0 to your computer and use it in GitHub Desktop.
Save sendelufa/5c50e55056b5b85f5e23a78314d237f0 to your computer and use it in GitHub Desktop.
Поиск минимального значение int в массиве int[]
public class FindMin {
public static void main(String[] args) {
System.out.println(findMin(new int[]{1, 2, 3})); // ожидается 1
System.out.println(findMin(new int[]{-1, -2, -3})); // ожидается -3
System.out.println(findMin(new int[]{5, 2, 34, 67})); // ожидается -2
System.out.println(findMin(new int[0])); // ожидается null
System.out.println(findMin(new int[]{})); // ожидается null
}
private static Integer findMin(int[] ints) {
if (ints.length == 0) {
return null;
}
var min = ints[0];
for (int i = 1; i < ints.length; i++) {
var current = ints[i];
if (current < min) {
min = current;
}
}
return min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment