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: Extrema1
- Function/Method: largestInt
- Parameters: int[]
- Returns: int
- Method signature (see below)
int largestInt (int[] vals)
(be sure your method is public)
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
- vals will have no more than 5000 elements
- The value of each element will be between Integer.MIN_VALUE and Integer.MAX_VALUE,
inclusive
Examples
-
[1, 2, 3, 4, 5, 6, 7]
Returns: 7
-
[-24, -7, -32]
Returns: -7
-
[]
Returns: 0
-
[0, -32767]
Returns: 0
JRNF
Last modified: Tue Sep 21 16:32:52 EDT 2004