Greatest element of a list

From Rosetta Code
Revision as of 10:31, 1 June 2008 by rosettacode>Spoon! (added a few languages)
Task
Greatest element of a list
You are encouraged to solve this task according to the task description, using any language you may know.

Create a function that returns the maximum value in a provided set of values, where the number of values isn't known until runtime.

C

This works well with floats. Replace with double, int or what-have-you before passing a different data type. <C>#include <assert.h>

float max(unsigned int count, float values[]) {

    float themax;
    unsigned int idx;
    assert(count > 0);
    themax = values[0];
    for(unsigned int idx = 1; idx < count; ++idx) {
         themax = values[idx] > themax ? values[idx] : themax;
    }
    return themax;

}</C>

C++

This will work for any type with a < operator defined. Uses the standard library function max_element(). <cpp>#include <algorithm>

  1. include <cassert>

template<typename Ty> Ty max(unsigned int count, Ty values[]) {

    assert(count > 0);
    return *std::max_element(values, values + count);

}</cpp>

Haskell

The built-in Haskell function maximum already does this.

my_max = maximum

It can alternately be defined as a "fold" on the built-in two-argument max function.

my_max = foldl1 max

Java

The first function works with arrays of floats. Replace with arrays of double, int, or other primitive data type. <java>public static float max(float[] values) {

   if (values.length == 0)
       throw new NoSuchElementException();
   float themax = values[0];
   for (int idx = 1; idx < values.length; ++idx) {
       if (values[idx] < themax)
           themax = values[idx];
   }
   return themax;

}</java>

The following functions work with arrays or Lists of reference types, respectively. Note that the type is required to implement Comparable, to ensure we can compare them. For Lists, there is a utility method Collections.max() that already does this. <java>public static <T extends Comparable<? super T>> T max(T[] values) {

   if (values.length == 0)
       throw new NoSuchElementException();
   T themax = values[0];
   for (int idx = 1; idx < values.length; ++idx) {
       if (values[idx].compareTo(themax) > 0)
           themax = values[idx];
   }
   return themax;

}

import java.util.List; import java.util.Collections;

public static <T extends Comparable<? super T>> T max(List<T> values) {

   return Collections.max(values);

}</java>

OCaml

<ocaml>exception Empty_list let rec my_max = function

  []      -> raise Empty_list
| [x]     -> x
| x :: xs -> max x (my_max xs)</ocaml>

Perl

<perl>sub max {

    my @list = @_;
    my $themax = $list[0];
    foreach ( @list ) {
         $themax = $_ > $themax ? $_ : $themax;
    }
    return $themax;

}</perl>

or, using the module List::Util's max() function. <perl>using List::Util qw(max); sub my_max {

   return max(@_);

}</perl>

Python

The built-in Python function max() already does this. <python>def my_max(values):

   return max(values)</python>