Weekly Problem maxInt


Problem Statement

A common programming task is to find the extreme values (either largest or smallest) in a collection of values.

Write a function that takes an array of ints and returns the value of the largest element.

Definition

Class

public class Extrema1 { public int largestInt(int[] vals) { // TODO: fill in largestInt } }

Notes

If there are no elements in vals (i.e., vals.length == 0), then largestInt should return 0.

Constraints

Examples

  1.   [1, 2, 3, 4, 5, 6, 7]
    
    Returns:  7
    

  2.     
      [-24, -7, -32]
    
    Returns:  -7
    

  3.   []
    
    Returns:  0
    

  4.   [0, -32767]
    
    Returns:  0
    

JRNF
Last modified: Tue Sep 21 16:32:52 EDT 2004