Find Maximum and Minimum Values of a List in Java

The challenge

Make two functions, max and min that take a(n) array/vector of integers list as input and outputs, respectively, the largest and lowest number in that array/vector.

Examples

max({4,6,2,1,9,63,-134,566}) returns 566
min({-52, 56, 30, 29, -54, 0, -110}) returns -110
max({5}) returns 5
min({42, 54, 65, 87, 0}) returns 0

Additionally

  • You may consider that there will not be any empty arrays/vectors.

The solution in Java code

Option 1:

public class Solution {

  public int min(int[] list) {
    java.util.Arrays.sort(list);
    return list[0];
  }
  
  public int max(int[] list) {
    java.util.Arrays.sort(list);
    return list[list.length-1];
  }
}

Option 2:

import java.util.Arrays;

public class Solution {

  public int min(int[] list) {
    return Arrays.stream(list).min().getAsInt();
  }
  
  public int max(int[] list) {
    return Arrays.stream(list).max().getAsInt();
  }
}

Option 3:

public class Solution {

  public int min(int[] list) {
    int min = list[0];
    for (int number : list) {
      if (number < min) {
        min = number;
      }
    }
    return min;
  }
  
  public int max(int[] list) {
    int max = list[0];
    for (int number : list) {
      if (number > max) {
        max = number;
      }
    }
    return max;
  }
}

Test cases to validate our solution

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {

    private Solution solution = new Solution();
    private Random rand = new Random();
    
    @Test
    public void fixedMin()
    {
      assertEquals(-110, solution.min(new int[]{-52, 56, 30, 29, -54, 0, -110}));
      assertEquals(0, solution.min(new int[]{42, 54, 65, 87, 0}));
      assertEquals(1, solution.min(new int[]{1, 2, 3, 4, 5, 10}));
      assertEquals(-10, solution.min(new int[]{-1, -2, -3, -4, -5, -10}));
    }
    
    @Test
    public void fixedMax()
    {
      assertEquals(56, solution.max(new int[]{-52, 56, 30, 29, -54, 0, -110}));
      assertEquals(566, solution.max(new int[]{4,6,2,1,9,63,-134,566}));
      assertEquals(5, solution.max(new int[]{5}));
      assertEquals(555, solution.max(new int[]{534,43,2,1,3,4,5,5,443,443,555,555}));
    }
    
    @Test
    public void randomTests()
    {    
       for(int i = 0; i < 10; i++)
       {
            int randLng = 2 + (rand.nextInt(14) % 15);
            List<Integer> randList = new ArrayList<>();
            
            for(int l = 0;  l < randLng; l++)
            {
                randList.add(rand.nextInt());
            }
            
            assertEquals(Long.valueOf(Collections.min(randList)), 
                Long.valueOf(solution.min(randList.stream().mapToInt(e -> e).toArray())));
            assertEquals(Long.valueOf(Collections.max(randList)), 
                Long.valueOf(solution.max(randList.stream().mapToInt(e -> e).toArray())));
        }
    }    
}
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments