1private static int findMin(int[] array) {
2 int min = array[0];
3 for(int i=1;i<array.length;i++) {
4 if(min > array[i]) {
5 min = array[i];
6 }
7 }
8 return min;
9 }
1public static double arrayMax(double[] arr) {
2 double max = Double.NEGATIVE_INFINITY;
3
4 for(double cur: arr)
5 max = Math.max(max, cur);
6
7 return max;
8}
1package com.concerned.crossbill;
2
3import java.util.Arrays;
4
5public class Foo {
6
7 public int getMin(int[] numbers) {
8 return Arrays.stream(numbers).min().getAsInt();
9 }
10}
11// test class
12import org.junit.Test;
13import static org.junit.Assert.assertEquals;
14import com.concerned.crossbill.Foo;
15
16public class FooTest {
17 public void testGetMin() {
18 int[] numbers = new int[]{12, 10, 31, 30, 23, 4, 5, 5, 5, 5, 10, 40};
19
20 Foo foo = new Foo();
21 int result = foo.getMin(numbers);
22 int expResult = 4;
23
24 assertEquals(expResult, result);
25 }
26}
1Integer[] num = { 2, 4, 7, 5, 9 };
2 // using Collections.min() to find minimum element
3 // using only 1 line.
4 int min1 = Collections.min(Arrays.asList(num));
5
6 // using Collections.max() to find maximum element
7 // using only 1 line.
8 int max1 = Collections.max(Arrays.asList(num));
9 System.out.println(min1); // 2
10 System.out.println(max1); // 9
1import java.util.Random;
2
3public class Main {
4
5public static void main(String[] args) {
6 int a[] = new int [100];
7 Random rnd = new Random ();
8
9 for (int i = 0; i< a.length; i++) {
10 a[i] = rnd.nextInt(99-0)+0;
11 System.out.println(a[i]);
12 }
13
14 int max = 0;
15
16 for (int i = 0; i < a.length; i++) {
17 a[i] = max;
18
19
20 for (int j = i+1; j<a.length; j++) {
21 if (a[j] > max) {
22 max = a[j];
23 }
24
25 }
26 }
27
28 System.out.println("Max element: " + max);
29}
30}
1 Scanner input = new Scanner(System.in);
2 // Minimum And Maximum
3 int count = 0;
4 int min = 0;
5 int max = 0;
6 boolean bugSolved = true;
7 /* or we can use :
8 int min = Integer.MAX_VALUE;
9 int max = Integer.MIN_VALUE;
10 */
11
12 while (true){
13 int cnt = count++;
14 System.out.print("Enter Number #"+(cnt+1)+": ");
15 boolean isValid = input.hasNextInt();
16 if(isValid){
17 int num = input.nextInt();
18 /* if (bugSolved){
19 bugSolved = false;
20 min = num;
21 max = num;
22 } # Just remove this condition and
23 boolean (bugSolved) at the top, if you use
24 int min = Integer.MAX_VALUE and int max =
25 Integer.MIN_VALUE */
26 if (num < min) {
27 min = num;
28 }else if (num > max){
29 max = num;
30 }
31 }else{
32 System.out.println("Invalid input..");
33 break;
34 }
35 input.nextLine();
36 }
37 System.out.println("Min Number : " + min);
38 System.out.println("Max Number : " + max);