Greatest element of a list

From Rosetta Code
Revision as of 04:24, 11 April 2022 by Hout (talk | contribs) (→‎JS ES6)
Task
Greatest element of a list
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Create a function that returns the maximum value in a provided set of values,
where the number of values may not be known until run-time.

11l

11l already has a "Maximum Value" function. <lang 11l>max(values)</lang>

8th

<lang forth> [ 1.0, 2.3, 1.1, 5.0, 3, 2.8, 2.01, 3.14159 ] ' n:max 0 a:reduce . cr </lang> Output: 5

AArch64 Assembly

Works with: as version Raspberry Pi 3B version Buster 64 bits

<lang AArch64 Assembly> /* ARM assembly AARCH64 Raspberry PI 3B */ /* program rechMax64.s */

/*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc"

/*********************************/ /* Initialized data */ /*********************************/ .data szMessResult: .ascii "Max number is = @ rank = @ address (hexa) = @ \n" // message result

tTableNumbers: .quad 50

                 .quad 12
                 .quad -1000
                 .quad 40
                 .quad 255
                 .quad 60
                 .quad 254

.equ NBRANKTABLE, (. - tTableNumbers) / 8 // number table posts

/*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program

   ldr x1,qAdrtTableNumbers
   mov x2,0
   ldr x4,[x1,x2,lsl #3]    // load first number
   mov x3,x2                // save first indice
   add x2,x2,1              // increment indice

1:

   cmp x2,#NBRANKTABLE      // indice ? maxi
   bge 2f                   // yes -> end search
   ldr x0,[x1,x2,lsl #3]    // load other number
   cmp x0,x4                // > old number max
   csel x4,x0,x4,gt         // if > x4 = x0 else x4=x4
   csel x3,x2,x3,gt         // if > x3 = x2 else x3=x3
   add x2,x2,1              // increment indice
   b 1b                     // and loop

2:

   mov x0,x4
   ldr x1,qAdrsZoneConv
   bl conversion10S         // decimal conversion
   ldr x0,qAdrszMessResult
   ldr x1,qAdrsZoneConv
   bl strInsertAtCharInc    // insert result at first @ character
   mov x5,x0                // save address message
   mov x0,x3
   ldr x1,qAdrsZoneConv     // conversion rank maxi
   bl conversion10S         // decimal conversion
   mov x0,x5                // message address
   ldr x1,qAdrsZoneConv
   bl strInsertAtCharInc    // insert result at Second @ character
   mov x5,x0                // save message address
   ldr x0,qAdrtTableNumbers
   lsl x3,x3,3
   add x0,x0,x3
   ldr x1,qAdrsZoneConv     // conversion address maxi
   bl conversion16          // hexa conversion
   mov x0,x5
   ldr x1,qAdrsZoneConv
   bl strInsertAtCharInc    // insert result at third @ character
   bl affichageMess         // display message final


100: // standard end of the program

   mov x0,0                 // return code
   mov x8,EXIT              // request to exit program
   svc 0                    // perform the system call

qAdrtTableNumbers: .quad tTableNumbers qAdrszMessResult: .quad szMessResult qAdrsZoneConv: .quad sZoneConv

/******************************************************************/ /* conversion hexadecimal register */ /******************************************************************/ /* x0 contains value and x1 address zone receptrice */ conversion16:

   stp x0,lr,[sp,-48]!        // save  registres
   stp x1,x2,[sp,32]          // save  registres
   stp x3,x4,[sp,16]          // save  registres
   mov x2,#60                 // start bit position
   mov x4,#0xF000000000000000 // mask
   mov x3,x0                  // save entry value

1: // start loop

   and x0,x3,x4               // value register and mask
   lsr x0,x0,x2               // right shift
   cmp x0,#10                 // >= 10 ?
   bge 2f                     // yes
   add x0,x0,#48              // no is digit
   b 3f

2:

   add x0,x0,#55              // else is a letter A-F

3:

   strb w0,[x1],#1            // load result  and + 1 in address
   lsr x4,x4,#4               // shift mask 4 bits left
   subs x2,x2,#4              // decrement counter 4 bits <= zero  ?
   bge 1b                     // no -> loop

100: // fin standard de la fonction

   ldp x3,x4,[sp,16]          // restaur des  2 registres
   ldp x1,x2,[sp,32]          // restaur des  2 registres
   ldp x0,lr,[sp],48          // restaur des  2 registres
   ret    

/********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" </lang>

Output:
Max number is =  +255 rank = +4 address (hexa) = 000000000041051C

ACL2

<lang Lisp>(defun maximum (xs)

  (if (endp (rest xs))
      (first xs)
      (max (first xs)
           (maximum (rest xs)))))</lang>

Action!

<lang Action!>BYTE FUNC Max(BYTE ARRAY tab BYTE size)

 BYTE i,res
 res=tab(0)
 FOR i=1 TO size-1
 DO 
   IF res<tab(i) THEN
     res=tab(i)
   FI
 OD

RETURN (res)

PROC Main()

 BYTE i,m,size=[20]
 BYTE ARRAY tab(size)
 
 FOR i=0 TO size-1
 DO 
   tab(i)=Rand(0)
 OD
 Print("Array:")
 FOR i=0 TO size-1
 DO
   PrintF(" %I",tab(i))
 OD
 PutE()
 m=Max(tab,size)
 PrintF("Greatest: %I%E",m)

RETURN</lang>

Output:

Screenshot from Atari 8-bit computer

Array: 106 182 121 251 21 244 46 157 228 251 173 50 106 126 193 230 88 117 114 120
Greatest: 251

ActionScript

<lang ActionScript>function max(... args):Number { var curMax:Number = -Infinity; for(var i:uint = 0; i < args.length; i++) curMax = Math.max(curMax, args[i]); return curMax; }</lang>

Ada

The keys for this task are initializing the compared value to the 'First value of the element type, and use of an unconstrained array type. <lang ada>with Ada.Text_Io;

procedure Max_Test isco

  -- substitute any array type with a scalar element
  type Flt_Array is array (Natural range <>) of Float;
  
  -- Create an exception for the case of an empty array
  Empty_Array : Exception;
  
  function Max(Item : Flt_Array) return Float is
     Max_Element : Float := Float'First;
  begin
     if Item'Length = 0 then 
        raise Empty_Array;
     end if;
 
     for I in Item'range loop
        if Item(I) > Max_Element then
           Max_Element := Item(I);
        end if;
     end loop;
     return Max_Element;
  end Max;
   
  Buf : Flt_Array := (-275.0, -111.19, 0.0, -1234568.0, 3.14159, -3.14159);

begin

  Ada.Text_IO.Put_Line(Float'Image(Max(Buf)));

end Max_Test;</lang> A generic function Max to deal with any floating-point type. <lang ada>generic

  type Item is digits <>;
  type Items_Array is array (Positive range <>) of Item;

function Generic_Max (List : Items_Array) return Item;</lang> Implementation of: <lang ada>function Generic_Max (List : Items_Array) return Item is

  Result : Item := List (List'First);

begin

  for Index in List'First + 1..List'Last loop
     Result := Item'Max (Result, List (Index));
  end loop;
  return Result;

end Generic_Max;</lang> When the argument array is empty, Constraint_Error exception is propagated, because array indexing is checked in Ada. Note also use of the floating-type attribute Max.

Aime

<lang aime>integer lmax(list l) {

   integer max, x;
   max = l[0];
   for (, x in l) {
       if (max < x) {
           max = x;
       }
   }
   max;

}</lang> or <lang aime>integer lmax(list l) {

   integer max;
   max = l[0];
   l.ucall(max_i, 1, max);
   max;

}</lang>

ALGOL 68

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny

<lang algol68># substitute any array type with a scalar element # MODE FLT = REAL;

  1. create an exception for the case of an empty array #

PROC raise empty array = VOID:(

 GO TO except empty array

);

PROC max = ([]FLT item)FLT: BEGIN

  IF LWB item > UPB item THEN
     raise empty array; SKIP
  ELSE
    FLT max element := item[LWB item];
    FOR i FROM LWB item + 1 TO UPB item DO
      IF item[i] > max element THEN
        max element := item[i]
      FI
    OD;
    max element
  FI

END # max #;

test:(

 []FLT buf = (-275.0, -111.19, 0.0, -1234568.0, pi, -pi);
 print((max(buf),new line)) EXIT
 except empty array:
   SKIP

)</lang>

Output:
+3.14159265358979e  +0

ALGOL W

<lang algolw>begin

   % simple list type                                                       %
   record IntList( integer val; reference(IntList) next );
   % find the maximum element of an IntList, returns 0 for an empty list    %
   integer procedure maxElement( reference(IntList) value list ) ;
       begin
           integer maxValue;
           reference(IntList) listPos;
           maxValue := 0;
           listPos  := list;
           if listPos not = null then begin
               % non-empty list                                             %
               maxValue := val(listPos);
               listPos  := next(listPos);
               while listPos not = null do begin
                   if val(listPos) > maxValue then maxValue := val(listPos);
                   listPos := next(listPos)
               end while_listPos_ne_null ;
           end if_listPos_ne_null ;
           maxValue
       end maxElement ;
   % test the maxElement procedure                                          %
   write( maxElement( IntList( -767, IntList( 2397, IntList( 204, null ) ) ) ) )

end.</lang>

Output:
          2397  

Amazing Hopper

Version 1: <lang Amazing Hopper>

  1. include <hopper.h>

main:

 lst=0
 max=0
 file="datos.txt"
 {","}             toksep
 {file}            statsfile
 {file}            load
                   mov        (lst)
 {0}               reshape    (lst)
 {lst}             array      (SORT)
 [end]             get        (lst)
                   mov        (max)
 {"Maximo = "}
 {max}
 {"\n"}            print

exit(0) </lang> Version 2: <lang Amazing Hopper>

  1. include <hopper.h>
  2. define SIZE_LIST 100000

main:

 lst=-1
 max=0
 {SIZE_LIST}       rand array (lst)
                   mul by     (SIZE_LIST)
                   mov        (lst)
 {lst}             array      (SORT)
 [end]             get        (lst)
                   mov        (max)
 {"Maximo = "}
 {max}
 {"\n"}            print

exit(0) </lang>

Output:
Maximo = 99999.8

AntLang

<lang AntLang>max|range[10]</lang>

APL

<lang apl>LIST←2 4 6 3 8 ⌈/LIST</lang>

Output:
8

AppleScript

<lang AppleScript> max({1, 2, 3, 4, 20, 6, 11, 3, 9, 7})

on max(aList) set _curMax to first item of aList repeat with i in (rest of aList) if i > _curMax then set _curMax to contents of i end repeat return _curMax end max </lang>


To find the greatest elements of lists which may contain data types other than numbers, we can write a more generic maximumBy function, which returns the maximum value from an array containing a series of any consistent data type, and which takes a type-specific comparison function as an argument.

Translation of: JavaScript

<lang AppleScript> -- maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a on maximumByMay(f, xs)

   set cmp to mReturn(f)
   script max
       on |λ|(a, b)
           if cmp's |λ|(a, b) < 0 then
               b
           else
               a
           end if
       end |λ|
   end script
   
   foldl1May(max, xs)

end maximumByMay

-- TEST ----------------------------------------------------------------------- on run

   set lstWords to ["alpha", "beta", "gamma", "delta", "epsilon", ¬
       "zeta", "eta", "theta", "iota", "kappa", "|λ|", "mu"]
   
   set lstCities to [{name:"Shanghai", population:24.15}, ¬
       {name:"Karachi", population:23.5}, ¬
       {name:"Beijing", population:21.5}, ¬
       {name:"Tianjin", population:14.7}, ¬
       {name:"Istanbul", population:14.4}, ¬
       {name:"Lagos", population:13.4}, ¬
       {name:"Tokyo", population:13.3}]
   
   script population
       on |λ|(x)
           population of x
       end |λ|
   end script
   
   
   return catMaybes({¬
       maximumByMay(comparing(|length|), lstWords), ¬
       maximumByMay(comparing(|length|), {}), ¬
       maximumByMay(comparing(population), lstCities)})
   
   --> {"epsilon", {name:"Shanghai", population:24.15}}
   

end run


-- GENERIC FUNCTIONS ----------------------------------------------------------

-- catMaybes :: [Maybe a] -> [a] on catMaybes(mbs)

   script emptyOrListed
       on |λ|(m)
           if nothing of m then
               {}
           else
               {just of m}
           end if
       end |λ|
   end script
   concatMap(emptyOrListed, mbs)

end catMaybes

-- comparing :: (a -> b) -> (a -> a -> Ordering) on comparing(f)

   set mf to mReturn(f)
   script
       on |λ|(a, b)
           set x to mf's |λ|(a)
           set y to mf's |λ|(b)
           if x < y then
               -1
           else
               if x > y then
                   1
               else
                   0
               end if
           end if
       end |λ|
   end script

end comparing

-- concatMap :: (a -> [b]) -> [a] -> [b] on concatMap(f, xs)

   set acc to {}
   tell mReturn(f)
       repeat with x in xs
           set acc to acc & |λ|(contents of x)
       end repeat
   end tell
   return acc

end concatMap

-- foldl1May :: (a -> a -> a) -> [a] -> Maybe a on foldl1May(f, xs)

   set lng to length of xs
   if lng > 0 then
       if lng > 1 then
           tell mReturn(f)
               set v to item 1 of xs
               set lng to length of xs
               repeat with i from 2 to lng
                   set v to |λ|(v, item i of xs, i, xs)
               end repeat
               return just(v)
           end tell
       else
           just(item 1 of xs)
       end if
   else
       nothing("Empty list")
   end if

end foldl1May

-- just :: a -> Just a on just(x)

   {nothing:false, just:x}

end just

-- length :: [a] -> Int on |length|(xs)

   length of xs

end |length|

-- max :: Ord a => a -> a -> a on max(x, y)

   if x > y then
       x
   else
       y
   end if

end max

-- nothing :: () -> Nothing on nothing(msg)

   {nothing:true, msg:msg}

end nothing

-- Lift 2nd class handler function into 1st class script wrapper -- mReturn :: Handler -> Script on mReturn(f)

   if class of f is script then
       f
   else
       script
           property |λ| : f
       end script
   end if

end mReturn</lang>

Output:

<lang AppleScript>{"epsilon", {name:"Shanghai", population:24.15}}</lang>

Applesoft BASIC

<lang ApplesoftBASIC> 100 REMMAX

110 R$ = "":E$ = ""
120 L =  LEN (L$)
130 IF L = 0 THEN  RETURN
140 FOR I = 1 TO L
150     C$ =  MID$ (L$,I,1)
160     SP = C$ = " "
170     IF SP THEN  GOSUB 200
180     E$ = E$ + C$
190 NEXT I
200 C$ = ""
210 IF E$ = "" THEN  RETURN
220 V =  VAL (E$):V$ = R$
230 E$ = "":E = V$ = ""
240 IF E AND V = 0 THEN  RETURN
250 R$ =  STR$ (V)
260 IF E THEN  RETURN
270 R =  VAL (V$)
280 IF R < V THEN  RETURN
290 R$ = V$: RETURN</lang>

<lang ApplesoftBASIC>L$ = "1 2 3 4 20 6 11 3 9 7" GOSUB 100MAX PRINT R$</lang>

Output:
20

ARM Assembly

Works with: as version Raspberry Pi

<lang ARM Assembly>

/* ARM assembly Raspberry PI */ /* program rechMax.s */

/* Constantes */ .equ STDOUT, 1 @ Linux output console .equ EXIT, 1 @ Linux syscall .equ WRITE, 4 @ Linux syscall

/*********************************/ /* Initialized data */ /*********************************/ .data szMessResult: .ascii "Max number is = " @ message result sMessValeur: .fill 12, 1, ' '

                 .ascii " rank = "

sMessRank: .fill 12, 1, ' '

                 .ascii " address (hexa) = "

sMessAddress: .fill 12, 1, ' '

                  .asciz "\n"

tTableNumbers: .int 50

                     .int 12
                     .int -1000
                     .int 40
                     .int 255
                     .int 60
                     .int 254

.equ NBRANKTABLE, (. - tTableNumbers) / 4 @ number table posts

/*********************************/ /* UnInitialized data */ /*********************************/ .bss /*********************************/ /* code section */ /*********************************/ .text .global main main: @ entry of program

   push {fp,lr}      @ saves 2 registers 
   ldr r1,iAdrtTableNumbers
   mov r2,#0
   ldr r4,[r1,r2,lsl #2]
   mov r3,r2
   add r2,#1

1:

   cmp r2,#NBRANKTABLE

bge 2f ldr r0,[r1,r2,lsl #2] cmp r0,r4 movgt r4,r0 movgt r3,r2 add r2,#1 b 1b

2:

   mov r0,r4
   ldr r1,iAdrsMessValeur                
   bl conversion10S       @ call conversion
   mov r0,r3
   ldr r1,iAdrsMessRank                
   bl conversion10       @ call conversion
   ldr r0,iAdrtTableNumbers
   add r0,r3,lsl #2
   ldr r1,iAdrsMessAddress                
   bl conversion16       @ call conversion
   ldr r0,iAdrszMessResult
   bl affichageMess            @ display message



100: @ standard end of the program

   mov r0, #0                  @ return code
   pop {fp,lr}                 @restaur 2 registers
   mov r7, #EXIT              @ request to exit program
   swi 0                       @ perform the system call

iAdrtTableNumbers: .int tTableNumbers iAdrsMessValeur: .int sMessValeur iAdrsMessRank: .int sMessRank iAdrsMessAddress: .int sMessAddress iAdrszMessResult: .int szMessResult

/******************************************************************/ /* display text with size calculation */ /******************************************************************/ /* r0 contains the address of the message */ affichageMess:

   push {fp,lr}    			/* save  registres */ 
   push {r0,r1,r2,r7}    		/* save others registers */
   mov r2,#0   				/* counter length */

1: /* loop length calculation */

   ldrb r1,[r0,r2]  			/* read octet start position + index */
   cmp r1,#0       			/* if 0 its over */
   addne r2,r2,#1   			/* else add 1 in the length */
   bne 1b          			/* and loop */
                               /* so here r2 contains the length of the message */
   mov r1,r0        			/* address message in r1 */
   mov r0,#STDOUT      		/* code to write to the standard output Linux */
   mov r7, #WRITE             /* code call system "write" */
   swi #0                      /* call systeme */
   pop {r0,r1,r2,r7}     		/* restaur others registers */
   pop {fp,lr}    				/* restaur des  2 registres */ 
   bx lr	        			/* return  */

/******************************************************************/ /* Converting a register to hexadecimal */ /******************************************************************/ /* r0 contains value and r1 address area */ conversion16:

   push {r1-r4,lr}    /* save registers */ 
   mov r2,#28         @ start bit position
   mov r4,#0xF0000000    @ mask
   mov r3,r0      @ save entry value

1: @ start loop

   and r0,r3,r4   @value register and mask
   lsr r0,r2      @ move right 
   cmp r0,#10      @ compare value
   addlt r0,#48        @ <10  ->digit	
   addge r0,#55        @ >10  ->letter A-F
   strb r0,[r1],#1  @ store digit on area and + 1 in area address
   lsr r4,#4       @ shift mask 4 positions
   subs r2,#4         @  counter bits - 4 <= zero  ?
   bge 1b	          @  no -> loop
   @end
   pop {r1-r4,lr}    @ restaur registres 
   bx lr             @return

/******************************************************************/ /* Converting a register to a decimal */ /******************************************************************/ /* r0 contains value and r1 address area */ conversion10:

   push {r1-r4,lr}    /* save registers */ 
   mov r3,r1
   mov r2,#10

1: @ start loop

   bl divisionpar10 @ r0 <- dividende. quotient ->r0 reste -> r1
   add r1,#48        @ digit	
   strb r1,[r3,r2]  @ store digit on area
   sub r2,#1         @ previous position
   cmp r0,#0         @ stop if quotient = 0 */
   bne 1b	          @ else loop
   @ and move spaves in first on area
   mov r1,#' '   @ space	

2:

   strb r1,[r3,r2]  @ store space in area
   subs r2,#1       @ @ previous position
   bge 2b           @ loop if r2 >= zéro 

100:

   pop {r1-r4,lr}    @ restaur registres 
   bx lr	          @return

/***************************************************/ /* Converting a register to a signed decimal */ /***************************************************/ /* r0 contains value and r1 area address */ conversion10S:

   push {r0-r4,lr}    @ save registers
   mov r2,r1       /* debut zone stockage */
   mov r3,#'+'     /* par defaut le signe est + */
   cmp r0,#0       @ negative number ? 
   movlt r3,#'-'   @ yes
   mvnlt r0,r0     @ number inversion
   addlt r0,#1   
   mov r4,#10       @ length area

1: @ start loop

   bl divisionpar10
   add r1,#48   @ digit
   strb r1,[r2,r4]  @ store digit on area
   sub r4,r4,#1      @ previous position
   cmp r0,#0          @ stop if quotient = 0
   bne 1b	
   strb r3,[r2,r4]  @ store signe 
   subs r4,r4,#1    @ previous position
   blt  100f        @ if r4 < 0 -> end
   mov r1,#' '   @ space	

2:

   strb r1,[r2,r4]  @store byte space
   subs r4,r4,#1    @ previous position
   bge 2b           @ loop if r4 > 0

100:

   pop {r0-r4,lr}   @ restaur registers
   bx lr  

/***************************************************/ /* division par 10 signé */ /* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/* /* and http://www.hackersdelight.org/ */ /***************************************************/ /* r0 dividende */ /* r0 quotient */ /* r1 remainder */ divisionpar10:

 /* r0 contains the argument to be divided by 10 */
  push {r2-r4}   /* save registers  */
  mov r4,r0 
  ldr r3, .Ls_magic_number_10 /* r1 <- magic_number */
  smull r1, r2, r3, r0   /* r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) */
  mov r2, r2, ASR #2     /* r2 <- r2 >> 2 */
  mov r1, r0, LSR #31    /* r1 <- r0 >> 31 */
  add r0, r2, r1         /* r0 <- r2 + r1 */
  add r2,r0,r0, lsl #2   /* r2 <- r0 * 5 */
  sub r1,r4,r2, lsl #1   /* r1 <- r4 - (r2 * 2)  = r4 - (r0 * 10) */
  pop {r2-r4}
  bx lr                  /* leave function */
  .align 4

.Ls_magic_number_10: .word 0x66666667


</lang>

Arturo

<lang rebol>arr: [5 4 2 9 7 3]

print max arr</lang>

Output:
9

AutoHotkey

CSV Data

<lang AutoHotkey>list = 1,5,17,-2 Loop Parse, list, `,

  x := x < A_LoopField ? A_LoopField : x

MsgBox Max = %x%</lang>

Pseudo-arrays

<lang AHK>list = 1,5,17,-2 StringSplit, list, list,`, ; creates a pseudo-array Loop % List0

  x := x < List%A_Index% ? List%A_Index% : x

MsgBox Max = %x%</lang>

True arrays

Works with: AutoHotkey_L

<lang AHK>List := [1,5,17,-2] For each, value in List

  x := x < value ? value : x

MsgBox Max = %x%</lang>

AWK

One-liner: <lang awk>$ awk 'func max(a){for(i in a)if(a[i]>r)r=a[i];return r}BEGIN{a[0]=42;a[1]=33;a[2]=21;print max(a)}' 42</lang>

More readable version: <lang awk>

  1. Usage: awk -f greatest_list_element.awk

function max(a) {

   for(i in a) if(a[i]>r) r=a[i];
   return r

}

BEGIN { a[0]=42;

       a[1]=33;
       a[2]=21;
       print max(a)
     }

</lang>

Axe

This example assumes the array is null-terminated so that the program can stop at the end of the data. <lang axe>Lbl MAX 0→M While {r₁}

{r₁}>M?{r₁}→M

End M Return</lang>

BASIC

Works with: QBasic

<lang qbasic>DECLARE SUB addVal (value AS INTEGER) DECLARE FUNCTION findMax% ()

REDIM SHARED vals(0) AS INTEGER DIM SHARED valCount AS INTEGER DIM x AS INTEGER, y AS INTEGER

valCount = -1

begin test run RANDOMIZE TIMER FOR x = 1 TO 10

   y = INT(RND * 100)
   addVal y
   PRINT y; " ";

NEXT PRINT ": "; findMax end test run

SUB addVal (value AS INTEGER)

   DIM tmp AS INTEGER
   IF valCount > -1 THEN
       'this is needed for BASICs that don't support REDIM PRESERVE
       REDIM v2(valCount) AS INTEGER
       FOR tmp = 0 TO valCount
           v2(tmp) = vals(tmp)
       NEXT
   END IF
   valCount = valCount + 1
   REDIM vals(valCount)
   IF valCount > 0 THEN
       'also needed for BASICs that don't support REDIM PRESERVE
       FOR tmp = 0 TO valCount - 1
           vals(tmp) = v2(tmp)
       NEXT
   END IF
   vals(valCount) = value

END SUB

FUNCTION findMax%

   DIM tmp1 AS INTEGER, tmp2 AS INTEGER
   FOR tmp1 = 0 TO valCount
       IF vals(tmp1) > tmp2 THEN tmp2 = vals(tmp1)
   NEXT
   findMax = tmp2

END FUNCTION</lang>

Output:
 8162   5139   7004   7393   5151   4476   577   4419   3333   4649  :  8162

BaCon

<lang freebasic>' Greatest element from list ' Populate sample array of numbers READ elements DECLARE numbers TYPE NUMBER ARRAY elements FOR i = 0 TO elements - 1

   READ numbers[i]

NEXT DATA 6 DATA 100,-2,300,4,500,6

' Demonstrate the function PRINT greatest(elements, numbers) END

' Return greatest element given count and list of numbers FUNCTION greatest(n, NUMBER a[])

   LOCAL mx = a[0]
   FOR i = 1 TO n - 1
       mx = MAX(mx, a[i])
   NEXT
   RETURN mx

END FUNCTION</lang>

Output:
prompt$  ./greatest-element
500

See also: BBC BASIC, Liberty BASIC, PowerBASIC, PureBasic, Run BASIC, TI-89 BASIC, Visual Basic

BASIC256

Translation of: Yabasic

<lang freebasic>l$ = "1,1234,62,234,12,34,6" dim n$(1) n$ = explode(l$, ",") m$ = "" : m = 0

for i = 1 to n$[?]-1 t$ = n$[i] if t$ > m$ then m$ = t$ if int(t$) > m then m = int(t$) next i

print "Alphabetic order: "; m$; ", numeric order: "; m</lang>

Batch File

<lang dos>::max.cmd @echo off setlocal enabledelayedexpansion set a=.%~1 if "%a%" equ "." set /p a="Input stream: " call :max res %a% echo %res% endlocal goto :eof

max

set %1=%2

loop

shift /2 if "%2" equ "" goto :eof if %2 gtr !%1! set res=%2 goto loop</lang>

Invocation from command line or from internal prompt

<lang dos>>max "123 456 3 234243 12" 234243

>max Input stream: 5 4 3 2 67 1 67</lang>

BBC BASIC

<lang bbcbasic> ListOfValues$ = "13, 0, -6, 2, 37, -10, 12"

     PRINT "Maximum value = " ; FNmax(ListOfValues$)
     END
     DEF FNmax(list$)
     LOCAL index%, number, max
     max = VAL(list$)
     REPEAT
       index% = INSTR(list$, ",", index%+1)
       number = VAL(MID$(list$, index%+1))
       IF number > max THEN max = number
     UNTIL index% = 0
     = max</lang>

bc

<lang bc>define m(a[], n) {

   auto m, i
   m = a[0]
   for (i = 1; i < n; i++) {
       if (a[i] > m) m = a[i]
   }
   return(m)

}</lang>

Befunge

<lang befunge>001pv <

   >&:01g`#v_1+#^_01g.@
   ^p10    <</lang>

Only works with positive integers. List must be terminated with -1.

BQN

<lang bqn>Max ← ⌈´</lang>

Output:
   Max ¯275‿¯111‿0‿¯1234568‿π‿¯π
3.141592653589793


Bracmat

When comparing two rational numbers, Bracmat compares numerically. In all other cases Bracmat compares lexically. <lang> ( biggest

 =   max
   .   !arg:
     |   !arg:%?max ?arg
       & !arg:? (%@:>!max:?max) (?&~)
     | !max
 )

& out$("1:" biggest$(5 100000 -5 aap 3446 NOOT mies 0)) & out$("2:" biggest$) & out

 $ ( "3:"
       biggest
     $ (5 100000 -5 43756243978569758/13 3365864921428443 87512487957139516/27 3446)
   )</lang>
Output:
1: mies
2:
3: 3365864921428443

Brat

Arrays have a max function, but here's a manual implementation. <lang brat>max = { list |

 list.reduce { n, max |
   true? n > max
     { max = n }
     { max }
 }

}

p max [3 4 1 2]</lang>

Burlesque

<lang burlesque> blsq ) {88 99 77 66 55}>] 99 </lang>

C

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

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

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

}</lang>

The following macro can be used with any number and type of arguments, provided that the arguments are simple, i.e. must not contain subexpressions where commas appear (this is because of the way the arguments are counted; the macro can be modified so that it is up to the caller to count the number of arguments passed).

Works with: GCC

<lang c>#include <stdarg.h>

  1. define MAX(A,...) ({ inline __typeof__ (A) _max_(__typeof__ (A) a, ...) {\
 va_list l; int i,c; const char *s = #__VA_ARGS__; __typeof__ (A) max = a;\
 __typeof__ (A) t;\
 for(c=1;*s!=0;s++) if (*s==',') c++;\
 va_start(l, a);\
 for(i=0;i<=c;i++) {\
 if ((t=va_arg(l,__typeof__ (A))) > max) max = t;\
 }\
 va_end(l); return max;\

}\ _max_((A),__VA_ARGS__);\ })</lang>

C#

C# already has a "Maximum Value" function.

<lang csharp>int[] values = new int[] {1,2,3,4,5,6,7,8,9,10};

int max = values.Max();</lang>

C++

A simple wrapper around the standard library function max_element(). Requires C++17. <lang cpp>#include <algorithm> //std::max_element

  1. include <iterator> //std::begin and std::end
  2. include <functional> //std::less

template<class It, class Comp = std::less<>>

   //requires ForwardIterator<It> && Compare<Comp>

constexpr auto max_value(It first, It last, Comp compare = std::less{}) {

   //Precondition: first != last
   return *std::max_element(first, last, compare);

}

template<class C, class Comp = std::less<>>

   //requires Container<C> && Compare<Comp>

constexpr auto max_value(const C& container, Comp compare = std::less{}) {

   //Precondition: !container.empty()
   using std::begin; using std::end;
   return max_value(begin(container), end(container), compare);

}</lang>

CFEngine

Note: CFEngine bundles are NOT functions, however they can behave in some ways that are similar to functions.

<lang cfengine3> bundle agent __main__ {

 vars:
     "number_of_list_elements"
       int => randomint( "0", 100 ),
       unless => isvariable( "$(this.promiser)" );
     "idx"
       slist => expandrange( "[0-$(number_of_list_elements)]", 1 ),
       unless => isvariable( "$(this.promiser)" );
     "number[$(idx)]"
       int => randomint( "0", "100" ),
       unless => isvariable( "$(this.promiser)" );
     "numbers" slist => sort( getvalues( number ), int );
 methods:
     "Get the greatest value"
       usebundle => greatest_value( @(numbers) ),
       useresult => "returned";
 reports:
     "'$(returned[max])' is the largest number in $(with)"
       with => join( ",", numbers );


} bundle agent greatest_value(list_of_values) {

  reports:
     "$(with)"
       with => max( list_of_values, int ),
       bundle_return_value_index => "max";

} </lang>

Output:
R: '97' is the largest number in 3,5,6,13,15,30,34,37,47,49,49,53,54,59,59,59,60,62,64,67,78,83,95,97

Clojure

The Clojure.core function max returns the max of its arguments. <lang clojure>(max 1 2 3 4) ; evaluates to 4

If the values are already in a collection, use apply

(apply max [1 2 3 4]) ; evaluates to 4</lang>

CLU

<lang clu>% This "maximum" procedure is fully general, as long as % the container type has an elements iterator and the % data type is comparable. % It raises an exception ("empty") if there are no elements.

maximum = proc [T,U: type] (a: T) returns (U)

         signals (empty)
         where T has elements: itertype (T) yields (U),
               U has gt: proctype (U,U) returns (bool)
   max: U
   seen: bool := false
   
   for item: U in T$elements(a) do
       if ~seen cor item > max then 
           max := item 
           seen := true
       end
   end
   if (~seen) then
       signal empty
   else
       return(max)
   end

end maximum


start_up = proc ()

   po: stream := stream$primary_output()
   
   % try it on an array of ints
   ints: array[int] := array[int]$[1,5,17,2,53,99,61,3]
   imax: int := maximum[array[int], int](ints)
   stream$putl(po, "maximum int: " || int$unparse(imax))    
   % try it on a sequence of reals
   reals: sequence[real] := sequence[real]$[-0.5, 2.6, 3.14, 2.72]
   rmax: real := maximum[sequence[real], real](reals)
   stream$putl(po, "maximum real: " || real$unparse(rmax))

end start_up</lang>

Output:
maximum int: 99
maximum real: 3.140000e+00

CMake

Only for lists of integers.

<lang cmake># max(var [value1 value2...]) sets var to the maximum of a list of

  1. integers. If list is empty, sets var to NO.

function(max var)

 set(first YES)
 set(choice NO)
 foreach(item ${ARGN})
   if(first)
     set(choice ${item})
     set(first NO)
   elseif(choice LESS ${item})
     set(choice ${item})
   endif()
 endforeach(item)
 set(${var} ${choice} PARENT_SCOPE)

endfunction(max)

set(list 33 11 44 22 66 55) max(maximum ${list}) message(STATUS "maximum of ${list} => ${maximum}")</lang>

-- maximum of 33;11;44;22;66;55 => 66

COBOL

This is already built into the language for tables of numbers. <lang cobol>DISPLAY FUNCTION MAX(nums (ALL))</lang>

A sample implementation: <lang cobol> IDENTIFICATION DIVISION.

      FUNCTION-ID. greatest-elt.
      DATA DIVISION.
      LOCAL-STORAGE SECTION.
      01  idx                     USAGE INDEX.
      01  Table-Len               CONSTANT 50.
      LINKAGE SECTION.
      01  num-table-area.
          03  num-table           PIC 9(8) OCCURS Table-Len TIMES.
      01  max-elt                 PIC 9(8).
      PROCEDURE DIVISION USING VALUE num-table-area RETURNING max-elt.
          PERFORM VARYING idx FROM 1 BY 1 UNTIL idx > Table-Len
              IF num-table (idx) > max-elt
                  MOVE num-table (idx) TO max-elt
              END-IF
          END-PERFORM
          GOBACK
          .
      END FUNCTION greatest-elt.</lang>

CoffeeScript

<lang coffeescript>

  1. using Math library

max1 = (list) ->

Math.max.apply null, list
  1. using no libraries

max2 = (list) ->

maxVal = list[0]
for value in list
 maxVal = value if value > maxVal
maxVal



  1. Test it

a = [0,1,2,5,4]; alert(max1(a)+". The answer is "+max2(a)); </lang>

ColdFusion

<lang cfm> <Cfset theList = '1, 1000, 250, 13'> <Cfparam name="maxNum" default=0> <Cfloop list="#theList#" index="i">

 <Cfif i gt maxNum><Cfset maxNum = i></Cfif>

</Cfloop> <Cfoutput>#maxNum#</Cfoutput> </lang>

<lang cfm> <Cfset theList = '1, 1000, 250, 13'> <Cfset maxNum = ListFirst(ListSort(thelist, "numeric", "desc"))> <Cfoutput>#maxNum#</Cfoutput> </lang>

Common Lisp

The built-in Common Lisp function max takes the max of all its arguments. <lang lisp>(max 1 2 3 4) (reduce #'max values) ; find max of a list (loop for x in values

     maximize x) ; alternative way to find max of a list</lang>

Component Pascal

BalckBox Component Builder <lang oberon2> MODULE Operations; IMPORT StdLog,Args,Strings;

PROCEDURE Max(s: ARRAY OF INTEGER): INTEGER; VAR i: INTEGER; max: INTEGER; BEGIN max := MIN(INTEGER); FOR i := 0 TO LEN(s) - 1 DO max := MAX(max,s[i]); END; RETURN max END Max;

PROCEDURE DoMax*; VAR sq: POINTER TO ARRAY OF INTEGER; p: Args.Params; i,n,done: INTEGER; BEGIN Args.Get(p); IF p.argc > 0 THEN NEW(sq,p.argc); FOR i := 0 TO p.argc - 1 DO Strings.StringToInt(p.args[i],n,done); sq[i] := n END; StdLog.String("max:> ");StdLog.Int(Max(sq));StdLog.Ln END END DoMax;

END Operations. </lang> Execute: ^Q Operations..DoMax 23 12 3 45 34 54 84 ~

Output:
max:>  84

Crystal

<lang ruby>values.max</lang>

D

D already has a "Maximum Element" function.

<lang d>void main() {

   import std.algorithm.searching : maxElement;
   import std.stdio : writeln;
   [9, 4, 3, 8, 5].maxElement.writeln;

}</lang>

Output:
9

Dart

<lang Dart>/*This is a function which returns the greatest element in a list of numbers */ num findGreatestElement(List<num> list){

 num greatestElement = list[0];
 for (num element in list){
   if (element>greatestElement) {
     greatestElement = element;
   }
 }  
 return greatestElement;

} /* and this is a shorter version */ import 'dart:math'; num findGreatestElement(List<num> list){

 return list.reduce(max);

} </lang>

dc

<lang dc>[sm llx] sg [lm p q] sq [d lm <u s_ z 0 =q llx] sl [d sm] su

["Put list of numbers on the stack starting here, then execute g"] s_

3.14159265358979 sp

_275.0 _111.19 0.0 _1234568.0 lp lp _1 *

lgx</lang>

Output:
3.14159265358979

DCL

<lang DCL>$ list = "45,65,81,12,0,13,-56,123,-123,888,12,0" $ max = f$integer( f$element( 0, ",", list )) $ i = 1 $ loop: $ element = f$element( i, ",", list ) $ if element .eqs. "," then $ goto done $ element = f$integer( element ) $ if element .gt. max then $ max = element $ i = i + 1 $ goto loop $ done: $ show symbol max</lang>

Output:
$ @greatest
  MAX = 888   Hex = 00000378  Octal = 00000001570

Delphi

<lang Delphi> program GElemLIst; {$IFNDEF FPC}

 {$Apptype Console}

{$ENDIF}

uses

 math;

const

 MaxCnt = 10000;

var

 IntArr : array of integer;
 fltArr : array of double;
 i: integer;

begin

 setlength(fltArr,MaxCnt); //filled with 0
 setlength(IntArr,MaxCnt); //filled with 0.0
 randomize;
 i := random(MaxCnt);      //choose a random place
 IntArr[i] := 1;
 fltArr[i] := 1.0;
 writeln(Math.MaxIntValue(IntArr)); // Array of Integer
 writeln(Math.MaxValue(fltArr));

end. </lang>

Dyalect

<lang dyalect>func max(xs) {

   var y
   for x in xs {
       if y == nil || x > y {
           y = x
       }
   }
   y

}

var xs = [1..10] max(xs)</lang>

Déjà Vu

<lang dejavu>max lst:

   lst! 0
   for item in copy lst:
        if > item dup:
             item drop

!. max [ 10 300 999 9 ]</lang>

Output:
999

Draco

<lang draco>/* Find the greatest element in an array of ints */ proc nonrec max([*] int a) int:

   int INT_MIN = ~((~0) >> 1);
   int nmax, i;
   nmax := INT_MIN;
   for i from 0 upto dim(a,1)-1 do
       if a[i] > nmax then nmax := a[i] fi
   od;
   nmax 

corp

/* Test on an array */ proc nonrec main() void:

   type arr = [8] int;
   writeln("Maximum: ", max(arr(1,5,17,2,53,99,61,3)))

corp</lang>

Output:
Maximum: 99

E

This function works for any value which responds to max/1:

<lang e>pragma.enable("accumulator") # non-finalized syntax feature

def max([first] + rest) {

   return accum first for x in rest { _.max(x) }

}</lang>

<lang e>? max([1, 2, 3])

  1. value: 3</lang>

To require only the comparison protocol, one needs to write out the algorithm a little more explicitly:

<lang e>def max([var bestSoFar] + rest) {

   for x ? (x > bestSoFar) in rest {
       bestSoFar := x
   }
   return bestSoFar

}</lang>

<lang e>? max([1, 3, 2])

  1. value: 3

? max([[1].asSet(), [2].asSet(), [1, 2].asSet()])

  1. value: [1, 2].asSet()</lang>

EasyLang

<lang>a[] = [ 2 9 4 3 8 5 ] for e in a[]

 max = higher e max

. print max</lang>

EchoLisp

<lang lisp>

a random length list of random values

(define L (map random (make-list (random 50) 100))) → L L → (24 60 83 8 24 60 31 97 96 65 9 41 64 24 22 57 73 17 6 28 77 58 18 13 27 22 41 69 85)

find max

(apply max L) → 97 </lang>

ECL

<lang ECL> MaxVal(SET OF INTEGER s) := MAX(s);

//example usage

SetVals := [4,8,16,2,1]; MaxVal(SetVals) //returns 16; </lang>

Efene

<lang efene>list_max = fn ([Head:Rest]) {

 list_max(Rest, Head)

}

list_max = fn ([], Res) {

 Res

} fn ([Head:Rest], Max) when Head > Max {

 list_max(Rest, Head)

} fn ([_Head:Rest], Max) {

 list_max(Rest, Max)

}

list_max1 = fn ([H:T]) {

 lists.foldl(fn erlang.max:2, H, T)

}

@public run = fn () {

   io.format("~p~n", [list_max([9, 4, 3, 8, 5])])
   io.format("~p~n", [list_max1([9, 4, 3, 8, 5])])

} </lang>

Eiffel

The GREATEST_ELEMENT class: <lang eiffel> class GREATEST_ELEMENT [G -> COMPARABLE]

create make

feature {NONE} --Implementation

is_max (element: G maximum: G): BOOLEAN do Result := maximum >= element end

max (list: ARRAY [G]): G require not_empty: not list.is_empty do Result := list [list.lower] across list as i loop Result := i.item.max (Result) end ensure is_part_of_array: list.has (Result) is_maximum: list.for_all (agent is_max(?, Result)) end

feature -- Initialization

make do end

greatest_element (a: ARRAY [G]): G do Result := max (a) end

end </lang> A test application: <lang eiffel> class APPLICATION

create make

feature {NONE} -- Initialization

make -- Run application. local numbers: ARRAY [INTEGER] greatest: GREATEST_ELEMENT [INTEGER] do create greatest.make numbers := <<1, 2, 3, 4, 5, 6, 7, 8, 9>> print (greatest.greatest_element (numbers)) end

end </lang>

Ela

<lang ela>open list

findBy p (x::xs) = foldl (\x y | p x y -> x | else -> y) x xs maximum = findBy (>)

maximum [1..10]</lang>

Elena

ELENA 5.0 : <lang elena>import extensions;

extension op {

   get Maximal()
   {
       auto en := cast Enumerator(self.enumerator());

       object maximal := nil;
       while (en.next())
       {
           var item := en.get();
           if (nil == maximal)
           {
               maximal := item
           } 
           else if (maximal < item)
           {
               maximal := item
           }
       };

       ^ maximal   
   }

}

public program() {

   console.printLine(new int[]{1,2,3,4,20,10,9,8}.Maximal)

}</lang>

Output:
20

Elixir

<lang elixir>iex(1)> Enum.max([3,1,4,1,5,9,2,6,5,3]) 9</lang>

Emacs Lisp

<lang lisp>(defun find-maximum (items)

 (let (max)
   (dolist (item items)
     (when (or (not max) (> item max))
       (setq max item)))
   max))

(find-maximum '(2 7 5)) ;=> 7</lang>

Built-in:

<lang lisp>(max '(2 7 5)) ;=> 7</lang>

Library: cl-lib

<lang lisp>(cl-loop for el in '(2 7 5) maximize el) ;=> 7 (cl-reduce #'max '(2 7 5)) ;=> 7</lang>

Library: seq.el

<lang lisp>(seq-max '(2 7 5)) ;=> 7</lang>

Erlang

Builtin. Using it from the Erlang shell: <lang erlang>>lists:max([9,4,3,8,5]). 9</lang>

ERRE

<lang ERRE> PROGRAM MAXLIST

! ! for rosettacode.org !

! VAR L$,EL$,CH$,I%,MAX

BEGIN

 PRINT(CHR$(12);) ! CLS
 INPUT("Lista",L$)
 L$=L$+CHR$(32)
 MAX=-1.7E+38
 FOR I%=1 TO LEN(L$) DO
   CH$=MID$(L$,I%,1)
   IF CH$<>CHR$(32) THEN ! blank is separator
      EL$=EL$+CH$
    ELSE
      IF VAL(EL$)>MAX THEN MAX=VAL(EL$) END IF
      EL$=""
   END IF
 END FOR
 PRINT("Max list element is";MAX)

END PROGRAM </lang> Note: The limit of this program is string variable lenght (255 chars). The advantage is no array use.

Euler Math Toolbox

<lang> >v=random(1,100); >max(v)

0.997492478596

</lang>

Euphoria

Applying a function to each element of an array

<lang Euphoria>function aeval( sequence sArr, integer id )

   for i = 1 to length( sArr ) do
       sArr[ i ] = call_func( id, { sArr[ i ] } )
   end for
   return sArr

end function

object biggun function biggest( object elem )

   if compare(elem, biggun) > 0 then
       biggun = elem
   end if
   return elem

end function

biggun = 0 object a a = aeval( {1,1234,62,234,12,34,6}, routine_id("biggest") ) printf( 1, "%d\n", biggun )

sequence s s = {"antelope", "dog", "cat", "cow", "wolf", "wolverine", "aardvark"} biggun = "ant" a = aeval( s, routine_id("biggest") ) printf( 1, "%s\n", {biggun} )</lang>

Output:
1234
wolverine

More trivial example

<lang euphoria>function get_biggest(sequence s)

   object biggun
   biggun = s[1]
   for i = 2 to length(s) do
       if compare(s[i], biggun) > 0 then
           biggun = s[i]
       end if
   end for
   return biggun

end function

constant numbers = {1,1234,62,234,12,34,6} printf(1,"%d\n",get_biggest(numbers))

constant animals = {"ant", "antelope", "dog", "cat", "cow", "wolf", "wolverine", "aardvark"} printf(1,"%s\n",{get_biggest(animals)})</lang>

Output:
1234
wolverine

Excel

Use the function MAX

<lang Excel> =MAX(3;2;1;4;5;23;1;2) </lang>

Output:
23

F#

I generate a list of 10 random numbers at runtime then use F#'s built in function to find the maximum value of the list.

<lang fsharp> let N = System.Random() let G = List.init 10 (fun _->N.Next()) List.iter (printf "%d ") G printfn "\nMax value of list is %d" (List.max G) </lang>

Output:
401566008 1378437959 1806806326 2010005455 1973773308 1216833747 268836584 1963610340 2120237482 1412806752 
Max value of list is 2120237482

Factor

The following word is in factor's standard library. <lang factor>: supremum ( seq -- elt ) [ ] [ max ] map-reduce ;</lang>

Fancy

<lang fancy>[1,-2,2,4,6,-4,-1,5] max println # => 6</lang>

Fantom

Has a built-in method to get maximum from a list.

<lang fantom> class Greatest {

 public static Void main () 
 {
   Int[] values := [1,2,3,4,5,6,7,8,9]
   Int greatest := values.max
   echo (greatest)
 }

} </lang>

Forth

<lang forth>: array-max ( addr len -- max )

 dup 0= if nip exit then
 over @  rot cell+  rot 1-
 cells bounds ?do  i @ max  cell +loop ;
stack-max ( n ... m count -- max ) 1 ?do max loop ;</lang>

Fortran

Works with: Fortran version 2003

The intrinsic function maxval returns the maximum value of the elements in an integer or real array:

<lang fortran>program test_maxval

integer,dimension(5),parameter :: x = [10,100,7,1,2] real,dimension(5),parameter :: y = [5.0,60.0,1.0,678.0,0.0]

write(*,'(I5)') maxval(x) write(*,'(F5.1)') maxval(y)

end program test_maxval</lang>

Output:
 100
 678.0

The intrinsic function max accepts any number of arguments. The type of these arguments can be integer, real, character, string of characters or arrays of these. <lang fortran>program test_max

 implicit none
 write (*, '(i0)') &
   & max (1, 2, 3)
 write (*, '(f3.1)') &
   & max (1.0, 2.0, 3.0)
 write (*, '(a)') &
   & max ('a', 'b', 'c')
 write (*, '(a)') &
   & max ('abc', 'bca', 'cab')
 write (*, '(i0, 2 (1x, i0))') &
   & max ([1, 8, 6], [7, 5, 3], [4, 2, 9])
 write (*, '(f3.1, 2 (1x, f3.1))') &
   & max ([1.0, 8.0, 6.0], [7.0, 5.0, 3.0], [4.0, 2.0, 9.0])
 write (*, '(a, 2 (1x, a))') &
   & max (['a', 'h', 'f'], ['g', 'e', 'c'], ['d', 'b', 'i'])
 write (*, '(a, 2 (1x, a))') &
   & max (['abc', 'hig', 'fde'], ['ghi', 'efd', 'cab'], ['def', 'bca', 'igh'])

end program test_max</lang>

Output:
 3
 3.0
 c
 cab
 7 8 9
 7.0 8.0 9.0
 g h i
 ghi hig igh

FreeBASIC

<lang freebasic>' FB 1.05.0 Win64

Function MaxElement(a() As Double) As Double

 Dim max As Double = a(LBound(a))
 For i As Integer = LBound(a) + 1 To UBound(a)
   If a(i) > max Then max = a(i)
 Next
 Return max

End Function

Dim As Integer i, n Input "How many values are to be input "; n If n < 1 Then End Dim a(1 To n) As Double For i = 1 To n

 Print "  Value"; i; " : ";
 Input "", a(i)

Next Dim max As Double = MaxElement(a()) Print Print "The greatest value is"; max Print Print "Press any key to quit" Sleep </lang>

Example of use :

Output:
How many values are to be input ? 4
  Value 1 : 70.5
  Value 2 : 23.67
  Value 3 : 150.2
  Value 4 : 145

The greatest value is 150.2

Frink

<lang frink> println[max1,2,3,5,10,20] </lang>

FunL

Using the pre-defined function max: <lang funl>println( max([1,2,3,-1,0]) )</lang>

Output:
3

It can be implemented as: <lang funl>def

 maximum( xs ) =
   def
     max( a, b )             = if a <= b then b else a
     foldl( f, z, [] )       = z
     foldl( f, z, x:xs )     = foldl( f, f(z, x), xs )
     foldl1( f, x:xs )       = foldl( f, x, xs )
     foldl1( _, [] )         = error( "foldl1: empty list" )
   foldl1( max, xs )</lang>

Futhark

<lang futhark>let main (xs: []f64) = reduce f64.max (-f64.inf) xs</lang>

This parallel formulation exploits the fact that negative infinity constitutes a neutral element for the maximum operator.

Fōrmulæ

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.

Programs in Fōrmulæ are created/edited online in its website, However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.

In this page you can see the program(s) related to this task and their results.

GW-BASIC

<lang gwbasic>10 INPUT "How many items? ", N% 20 DIM ARR(N%) 30 FOR I% = 0 TO N%-1 40 PRINT "Value of item #";I% 50 INPUT ARR(I%) 60 NEXT I% 70 CHAMP = ARR(0) : INDEX = 0 80 FOR I% = 1 TO N%-1 90 IF ARR(I%)>CHAMP THEN CHAMP=ARR(I%):INDEX=I% 100 NEXT I% 110 PRINT "The maximum value was ";CHAMP;" at index ";INDEX;"." 120 END</lang>

GAP

<lang gap># Built-in

L := List([1 .. 100], n -> Random(1, 10));

MaximumList(L);

  1. 10</lang>

Go

Library
<lang go>package main

import (

   "fmt"
   "github.com/gonum/floats"

)

func main() { fmt.Println(floats.Max([]float64{63, 70, 37, 34, 83, 27, 19, 97, 9, 17})) // prt 97 fmt.Println(floats.Min([]float64{63, 70, 37, 34, 83, 27, 19, 97, 9, 17})) // prt 9 }</lang>

<lang go>package main

import "fmt"

func main() { x := []int{ 48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17, }

smallest, biggest := x[0], x[0] for _, v := range x { if v > biggest { biggest = v } if v < smallest { smallest = v } }

fmt.Println("The biggest number is ", biggest) // prt 97 fmt.Println("The smallest number is ", smallest) // prt 9 }</lang>

List

The task title says list. This solution uses a Go slice as a list. <lang go>package main

import (

   "fmt"
   "math/rand"
   "time"

)

// function, per task description func largest(a []int) (lg int, ok bool) {

   if len(a) == 0 {
       return
   }
   lg = a[0]
   for _, e := range a[1:] {
       if e > lg {
           lg = e
       }
   }
   return lg, true

}

func main() {

   // random size slice
   rand.Seed(time.Now().UnixNano())
   a := make([]int, rand.Intn(11))
   for i := range a {
       a[i] = rand.Intn(101) - 100 // fill with random numbers
   }
   fmt.Println(a)
   lg, ok := largest(a)
   if ok {
       fmt.Println(lg)
   } else {
       fmt.Println("empty list.  no maximum.")
   }

}</lang>

Set

The task description says set. This solution uses a Go map as a set. <lang go>package main

import (

   "fmt"
   "math"
   "math/rand"
   "time"

)

// Function, per task description. Interesting with the float64 type because // of the NaN value. NaNs do not compare to other values, so the result of // a "largest" function on a set containing a NaN might be open to // interpretation. The solution provided here is to return the largest // of the non-NaNs, and also return a bool indicating the presense of a NaN. func largest(s map[float64]bool) (lg float64, ok, nan bool) {

   if len(s) == 0 {
       return
   }
   for e := range s {
       switch {
       case math.IsNaN(e):
           nan = true
       case !ok || e > lg:
           lg = e
           ok = true
       }
   }
   return

}

func main() {

   rand.Seed(time.Now().UnixNano())
   // taking "set" literally from task description
   s := map[float64]bool{}
   // pick number of elements to add to set
   n := rand.Intn(11)
   // add random numbers, also throw in an occasional NaN or Inf.
   for i := 0; i < n; i++ {
       switch rand.Intn(10) {
       case 0:
           s[math.NaN()] = true
       case 1:
           s[math.Inf(1)] = true
       default:
           s[rand.ExpFloat64()] = true
       }
   }
   fmt.Print("s:")
   for e := range s {
       fmt.Print(" ", e)
   }
   fmt.Println()
   switch lg, ok, nan := largest(s); {
   case ok && !nan:
       fmt.Println("largest:", lg)
   case ok:
       fmt.Println("largest:", lg, "(NaN present in data)")
   case nan:
       fmt.Println("no largest, all data NaN")
   default:
       fmt.Println("no largest, empty set")
   }

}</lang>

Golfscript

<lang golfscript>{$-1=}:max; [1 4 8 42 6 3]max # Example usage</lang>

Groovy

<lang groovy>println ([2,4,0,3,1,2,-12].max())</lang>

Output:
4

Haskell

The built-in Haskell function maximum returns a maximum based on default comparison between members of an ordered type. <lang haskell>my_max = maximum</lang> It can alternately be defined as a "fold" on the built-in two-argument max function. <lang haskell>my_max = foldl1 max</lang>

More generally, maximum is a special case of maximumBy, which allows us to define or supply our own comparison function, and define the particular type of maximum that we need: <lang haskell>import Data.List (maximumBy) import Data.Ord (comparing)

wds :: [String] wds = ["alpha", "beta", "gamma", "delta", "epsilon", "zeta"]

main :: IO () main = print $ maximumBy (comparing length) wds</lang>

As a fold, maximumBy could be defined along the lines of:

<lang haskell>maximumBy

 :: Foldable t
 => (a -> a -> Ordering) -> t a -> a

maximumBy cmp =

 let max_ x y =
       case cmp x y of
         GT -> x
         _ -> y
 in foldr1 max_</lang>

hexiscript

<lang hexiscript>fun greatest a

 let l len a
 let max a[0]
 for let i 1; i < l; i++
   if max < a[i]
     let max a[i]
   endif
 endfor
 return max

endfun</lang>

HicEst

<lang hicest>

  max_value = MAX( -123,  234.56, 345.678, -456E3, -455) ! built-in function MAX(...)

! or for an array:

  max_value = MAX( array_of_values )

! or to find a maximum value in a file named filename:

  CHARACTER List, filename='Greatest element of a list.hic' ! filename contains this script
  REAL values(1) ! unknown number of values, allocate more below
  OPEN(FIle=filename, BINary, LENgth=len)
  ALLOCATE(values, len/2) ! number of values <= half byte count of file
  ! read all values, returns item count in values_found:
  READ(FIle=filename, ItemS=values_found, CLoSe=1) values ! no Format needed for plain text numbers
  max_value = MAX(values)
  ! write values found in filename and result to spreadsheet type dialog window:
  DLG(Text=values, Text=max_value, TItle=values_found)
  WRITE(ClipBoard, Name) max_value, values_found, values ! pasted to line below
  ! max_value=345.678; values_found=30; values(1)=-123; values(2)=234.56; values(3)=345.678; values(4)=-456E3; values(5)=-455; values(6)=1; values(7)=2; values(8)=1; values(9)=0; values(10)=0; ...truncated
END

</lang>

Hoon

<lang Hoon>:- %say |= [^ [a=(list ,@) ~] ~]

- %noun
 (snag 0 (sort a gte))</lang>

Usage: Add to a file gen/max.hoon

> +max [1 2 3 ~]
3

i

<lang i>concept largest(l) { large = l[0] for element in l if element > large large = element end end return large }

software { print(largest([23, 1313, 21, 35757, 4, 434, 232, 2, 2342])) }</lang>

Icon and Unicon

<lang icon>procedure main()

  local l
  l := [7,8,6,9,4,5,2,3,1]
  write(max(l))

end

procedure max(l)

  local max
  max := l[1]
  every max <:= !l
  return max

end</lang>

IS-BASIC

<lang IS-BASIC>1000 DEF FINDMAX(REF ARR) 1010 LET MX=ARR(LBOUND(ARR)) 1020 FOR I=LBOUND(ARR)+1 TO UBOUND(ARR) 1030 LET MX=MAX(MX,ARR(I)) 1040 NEXT 1050 LET FINDMAX=MX 1060 END DEF</lang>

J

Solution:<lang j> >./</lang> Example:<lang J> >./ 1 2 3 2 1 3

  >./  NB.  Maximum value of an empty list = identity element (or neutral) of max = -∞

__</lang>

Janet

<lang janet> (def elems @[3 1 3 2])

  1. Use extreme function from stdlib with > function.

(extreme > elems)

  1. Unpack list as arguments to max function.

(max ;elems) </lang>

Java

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

   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;

}</lang>

Optionally, if it is OK to rearrange the contents of the original array: <lang java>public static float max(float[] values) throws NoSuchElementException {

   if (values.length == 0)
       throw new NoSuchElementException();
   Arrays.sort(values);//sorts the values in ascending order
   return values[values.length-1];

}</lang>

The following functions work with Lists or arrays 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. For arrays, we can just use the Arrays.asList() wrapper to wrap it into a list and then use the function for lists. <lang java>import java.util.List; import java.util.Collections; import java.util.Arrays;

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

   return Collections.max(values);

}

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

   return Collections.max(Arrays.asList(values));

}</lang>

JavaScript

ES3-5

<lang javascript>Math.max.apply(null, [ 0, 1, 2, 5, 4 ]); // 5</lang>

ES 5 maxima beyond simple numeric data types

Math.max() serves well with simple numeric types, but for less restricted use we can write a generic maximumBy function which returns the maximum value from an array containing a series of any consistent data type, and which takes a type-specific comparison function as an argument.

<lang JavaScript>(function () {

   // (a -> a -> Ordering) -> [a] -> a 
   function maximumBy(f, xs) {
       return xs.reduce(function (a, x) {
           return a === undefined ? x : (
               f(x, a) > 0 ? x : a
           );
       }, undefined);
   }
   // COMPARISON FUNCTIONS FOR SPECIFIC DATA TYPES
   //Ordering: (LT|EQ|GT)
   //  GT: 1 (or other positive n)
   //  EQ: 0
   //  LT: -1 (or other negative n) 
   function wordSortFirst(a, b) {
       return a < b ? 1 : (a > b ? -1 : 0)
   }
   function wordSortLast(a, b) {
       return a < b ? -1 : (a > b ? 1 : 0)
   }
   function wordLongest(a, b) {
       return a.length - b.length;
   }
   function cityPopulationMost(a, b) {
       return a.population - b.population;
   }
   function cityPopulationLeast(a, b) {
       return b.population - a.population;
   }
   function cityNameSortFirst(a, b) {
       var strA = a.name,
           strB = b.name;
       return strA < strB ? 1 : (strA > strB ? -1 : 0);
   }
   function cityNameSortLast(a, b) {
       var strA = a.name,
           strB = b.name;
       return strA < strB ? -1 : (strA > strB ? 1 : 0);
   }
   var lstWords = [
           'alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta',
           'theta', 'iota', 'kappa', 'lambda'
       ];
   var lstCities = [
       {
           name: 'Shanghai',
           population: 24.15
           }, {
           name: 'Karachi',
           population: 23.5
           }, {
           name: 'Beijing',
           population: 21.5
           }, {
           name: 'Tianjin',
           population: 14.7
           }, {
           name: 'Istanbul',
           population: 14.4
           }, , {
           name: 'Lagos',
           population: 13.4
           }, , {
           name: 'Tokyo',
           population: 13.3
           }
       ];
   return [
       maximumBy(wordSortFirst, lstWords),
       maximumBy(wordSortLast, lstWords),
       maximumBy(wordLongest, lstWords),
       maximumBy(cityPopulationMost, lstCities),
       maximumBy(cityPopulationLeast, lstCities),
       maximumBy(cityNameSortFirst, lstCities),
       maximumBy(cityNameSortLast, lstCities)
   ]

})();</lang>


Output:

<lang JavaScript>[

 "alpha",
 "zeta",
 "epsilon",
 {
   "name": "Shanghai",
   "population": 24.15
 },
 {
   "name": "Tokyo",
   "population": 13.3
 },
 {
   "name": "Beijing",
   "population": 21.5
 },
 {
   "name": "Tokyo",
   "population": 13.3
 }

]</lang>

ES6

For, numbers, a method of the standard Math object: <lang javascript>Math.max(...[ 0, 1, 2, 5, 4 ]); // 5</lang>

and for orderings defined over other datatypes: <lang JavaScript>(() => {

   "use strict";
   // ----------- GREATEST ELEMENT OF A LIST ------------
   // maximumByMay :: (a -> a -> Ordering) ->
   // [a] -> Maybe a
   const maximumByMay = f =>
       // Nothing, if the list is empty,
       // or just the maximum value when compared
       // in terms of f.
       xs => Boolean(xs.length) ? (
           Just(xs.slice(1).reduce(
               (a, x) => 0 < f(a)(x) ? (
                   a
               ) : x,
               xs[0]
           ))
       ) : Nothing();


   // ---------------------- TEST -----------------------
   const main = () =>
       JSON.stringify(
           catMaybes([
               maximumByMay(
                   comparing(x => x.length)
               )([
                   "alpha", "beta", "gamma", "delta",
                   "epsilon", "zeta", "eta"
               ]),
               maximumByMay(comparing(x => x.length))([]),
               maximumByMay(comparing(x => x.n))([{
                   k: "Bejing",
                   n: 21.5
               }, {
                   k: "Delhi",
                   n: 16.7
               }, {
                   k: "Karachi",
                   n: 23.5
               }, {
                   k: "Lagos",
                   n: 16.0
               }, {
                   k: "Shanghai",
                   n: 24.3
               }, {
                   k: "Tokyo",
                   n: 13.2
               }])
           ]),
           null, 2
       );
   // --------------------- GENERIC ---------------------
   // Just :: a -> Maybe a
   const Just = x => ({
       type: "Maybe",
       Nothing: false,
       Just: x
   });


   // Nothing :: Maybe a
   const Nothing = () => ({
       type: "Maybe",
       Nothing: true
   });


   // catMaybes :: [Maybe a] -> [a]
   const catMaybes = mbs =>
       mbs.flatMap(
           m => m.Nothing ? (
               []
           ) : [m.Just]
       );


   // comparing :: (a -> b) -> (a -> a -> Ordering)
   const comparing = f =>
       x => y => {
           const
               a = f(x),
               b = f(y);
           return a < b ? -1 : (a > b ? 1 : 0);
       };
   // MAIN ---
   return main();

})();</lang>

Output:
[
  "epsilon",
  {
    "k": "Shanghai",
    "n": 24.3
  }
]

jq

jq defines a total ordering of all JSON entities, and the max filter can accordingly be used on any JSON array: <lang jq>[1, 3, 1.0] | max # => 3

[ {"a": 1}, {"a":3}, {"a":1.0}] | max # => {"a": 3}</lang> Warning: both "[null]|max" and "[]|max" yield null.

Given a stream, s, of JSON values, the following filter will emit null if the stream is empty, or the maximum value otherwise: <lang jq>reduce s as $x (null; if $x > . then $x else . end)</lang>

Julia

Using the built-in maximum function: <lang julia>julia> maximum([1,3,3,7]) 7

julia> maximum([pi,e+2/5,cos(6)/5,sqrt(91/10)]) 3.141592653589793

julia> maximum([1,6,Inf]) Inf

julia> maximum(Float64[]) maximum: argument is empty at In[138]:1

in maximum at abstractarray.jl:1591

</lang> (Note that it throws an exception on an empty array.)

K

<lang k> |/ 6 1 7 4 7</lang>

Klingphix

<lang Klingphix>include ..\Utilitys.tlhy

( "1" "1234" "62" "234" "12" "34" "6" )

dup "Alphabetic order: " print lmax ?

f tonum ;

@f map

"Numeric order: " print lmax ?

" " input</lang>

Klong

<lang k>list::[ 1.0 2.3 1.1 5.0 3 2.8 2.01 3.14159 77 ] |/list |/ [ 1.0 2.3 1.1 5.0 3 2.8 2.01 3.14159 66 ] |/ 1.0,2.3,1.1,5.0,3,2.8,2.01,3.14159,55</lang>

Output:
77
66
55

Kotlin

Kotlin already has a 'max' function in its standard library so we use that: <lang scala>// version 1.0.5-2 fun main(args: Array<String>) {

   print("Number of values to be input = ")
   val n = readLine()!!.toInt()
   val array = DoubleArray(n)
   for (i in 0 until n) {
        print("Value ${i + 1} = ")
        array[i] = readLine()!!.toDouble()
   }
   println("\nThe greatest element is ${array.max()}")

}</lang> Example of use:

Output:
Number of values to be input = 4
Value 1 = 70.5
Value 2 = 23.67
Value 3 = 150.2
Value 4 = 145

The greatest element is 150.2

Lambdatalk

<lang Scheme> 1) using the builtin primitive

{max 556 1 7344 4 7 52 22 55 88 122 55 99 1222 578} -> 7344

2) buidling a function

{def my-max

{def max-h
 {lambda {:l :greatest}
  {if {A.empty? :l}
   then :greatest
   else {max-h {A.rest :l} 
               {if {> {A.first :l} :greatest}
                then {A.first :l}
                else :greatest}}}}}
{lambda {:l}
 {if {A.empty? :l} then empty else {max-h :l {A.first :l}}}}}

-> my-max

{my-max {A.new 556 1 7344 4 7 52 22 55 88 122 55 99 1222 578}} -> 7344 </lang>

Lasso

<lang Lasso>define greatest(a::array) => { return (#a->sort&)->last }

local(x = array(556,1,7344,4,7,52,22,55,88,122,55,99,1222,578)) greatest(#x)</lang>

Output:
7344

LFE

Builtin. Using it from the LFE shell: <lang lisp>>(: lists max '[9 4 3 8 5]) 9</lang>

Liberty BASIC

<lang lb>aList$= "1 15 -5 6 39 1.5 14"

maxVal = val(word$(aList$, 1)) token$ = "?" while token$ <> ""

   index = index + 1
   token$ = word$(aList$, index)
   aVal = val(token$)
   if aVal > maxVal then maxVal = aVal

wend

print "maxVal = ";maxVal</lang>

Lingo

<lang lingo>l = [1,7,5] put max(l) -- 7</lang>

LiveCode

Max is built-in<lang livecode>put max(2,3,6,7,4,1)</lang> Result: 7

To be fair to the exercise, an alternative implementation could be<lang liveCode>function max2 lst

   local maxNum
   put item 1 of lst into maxNum
   repeat with n = 1 to the number of items of lst
       if item n of lst > maxNum then
           put item n of lst into maxNum
       end if
   end repeat
   return maxNum

end max2 on mouseUp

answer max2("1,2,5,6,7,4,3,2")

end mouseUp</lang>

Works with: UCB Logo

If the template is, like SUM, the name of a procedure that is capable of accepting arbitrarily many inputs, it is more efficient to use APPLY instead of REDUCE. The latter is good for associative procedures that have been written to accept exactly two inputs: <lang logo>to max :a :b output ifelse :a > :b [:a] [:b] end

print reduce "max [...]</lang>

Alternatively, REDUCE can be used to write MAX as a procedure that accepts any number of inputs, as SUM does: <lang logo>to max [:inputs] 2 if emptyp :inputs ~

  [(throw "error [not enough inputs to max])]

output reduce [ifelse ?1 > ?2 [?1] [?2]] :inputs end</lang> ==Logo==

Works with: UCB Logo

<lang logo>to bigger :a :b

 output ifelse [greater? :a :b] [:a] [:b]

end

to max :lst

 output reduce "bigger :lst

end</lang>

Logtalk

<lang logtalk> max([X| Xs], Max) :-

   max(Xs, X, Max).

max([], Max, Max). max([X| Xs], Aux, Max) :-

   (   X @> Aux ->
       max(Xs, X, Max)
   ;   max(Xs, Aux, Max)
   ).</lang>

Lua

<lang lua>-- Table to store values local values = {} -- Read in the first number from stdin local new_val = io.read"*n" -- Append all numbers passed in -- until there are no more numbers (io.read'*n' = nil) while new_val do

 values[#values+1] = new_val
 new_val = io.read"*n"

end

-- Print the max print(math.max(unpack(values))) </lang>

M2000 Interpreter

<lang M2000 Interpreter> Module TestThis {

     Print "Search a tuple type list (is an array also)"
     A=(,)
     For i=1 to Random(1,10)
     Append A, (Random(1,100),)
     Next
     Print Len(A)
     Print A
     Print A#max()
     
     Print "Search an array"
     B=lambda->Random(1,100)
     Rem Dim A(1 to Random(1,10))<<B()
     Dim A(1 to Random(1,10))<<lambda->{=Random(1,100)}()
     Print Len(A())
     Print A()
     Print A()#max()
     
     \\ #max() skip non numeric values
     Rem Print (1,"100",3)#max()=3
     
     Print "Search an inventory list"
     Inventory C
     for i=1 to Random(1,10)
           do
                 key=random(10000)
           until not exist(c, key)
           \\ we can put a number as string
           if random(1,2)=1 then Append c, key:=B() else Append c, key:=str$(B())
     Next
     
     \\ if inventory item is string with a number work fine
     Function MaxItem(a) {
           k=each(a,2)
           val=a(0!)
           while k
                 \\ using stack of values
                 \\ over -equal to over 1 - copy value from 1 to top, means double the top value
                 \\ number - pop top value
                 \\ drop -equal to drop 1 : drop top value 
                 Push a(k^!): Over : If Number>val then Read Val else drop
                 Rem If a(k^!)>Val Then Val=a(k^!)
           end while
           =val
     }
     Print Len(C)
     Print C
     Print MaxItem(C)
     
     Print "Search a stack object"
     \\ a stack object is the same as the stack of values
     \\ which always is present
     D=stack
     I=0
     J=Random(1,10)
     \\ Stack stackobjext {}
     \\ hide current stack and attach the D stack
     Stack D {
           Push B() : I++ : IF I>J Else Loop
     }
     \\ if stack item isn't numeric we get a run time error
     Function MaxItemStack(a) {
           Stack a {env$=envelope$()}
           if replace$("N","", env$)<>"" then error "only numbers allowed"
           k=each(a,2)
           val=Stackitem(a,1)
           while k
                 If Stackitem(k)>val then Val=stackitem(k)
           end while
           =val
     }
     Print Len(D)
     Print D
     Print MaxItemStack(D)     	

} TestThis </lang>

Maple

This is a built-in, polymorphic procedure in Maple. <lang Maple>> max( { 1, 2, Pi, exp(1) } ); # set

                                  Pi

> max( [ 1, 2, Pi, exp(1) ] ); # list

                                  Pi

> max( 1, 2, Pi, exp(1) ); # sequence

                                  Pi

> max( Array( [ 1, 2, Pi, exp(1) ] ) ); # Array

                                  Pi</lang>

For numeric data in (multi-dimensional) rtables, a particularly flexible and powerful method for finding the maximum (and many other things) is the use of "rtable_scanblock". The maximum of an Array is a built-in rtable_scanblock operation and can be found as follows. <lang Maple>> A := Array([1,2,4/5,3,11]): rtable_scanblock( A, [rtable_dims(A)], Maximum );

                                  11</lang>

Mathematica / Wolfram Language

Input: <lang Mathematica>Max[1, 3, 3, 7] Max[Pi,E+2/5,17 Cos[6]/5,Sqrt[91/10]] Max[1,6,Infinity] Max[]</lang>

Output:
 7
 17 Cos[6]/5
 Infinity
-Infinity

Note that Max returns minus infinity if supplied with no arguments; as it should: <lang Mathematica>Max[Max[],Max[a,b,c]] Max[Max[a],Max[b,c]] Max[Max[a,b],Max[c]] Max[Max[a,b,c],Max[]]</lang> should all give the same results, therefore max[] should give -Infinity. If it WOULD give 0 strange things can happen: <lang Mathematica>Max[Max[], Max[-4, -3]]</lang> WOULD give 0 instead of -3

MATLAB

<lang Matlab>function [maxValue] = findmax(setOfValues)

  maxValue = max(setOfValues);</lang>

Maxima

<lang maxima>u : makelist(random(1000), 50)$

/* Three solutions */ lreduce(max, u);

apply(max, u);

lmax(u);</lang>

MAXScript

MAXScript has a built-in function called amax(), which will return the maximum of an array or the values supplied to it. The following custom function will return the maximum of the array supplied to it, or 'undefined' if an empty array is supplied. <lang MAXScript>fn MaxValue AnArray = ( if AnArray.count != 0 then ( local maxVal = 0 for i in AnArray do if i > maxVal then maxVal = i maxVal ) else undefined )</lang>

Metafont

The max macro (in the base set of macro for Metafont) accepts any number of arguments, and accepts both numerics (numbers), pairs (bidimensional vectors), and strings (not mixed).

<lang metafont>show max(4,5,20,1); show max((12,3), (10,10), (25,5)); show max("hello", "world", "Hello World");</lang>

min

Works with: min version 0.19.3

<lang min>(

 'bool                             ;does the list have any elements?
 (-inf ('> 'pop 'nip if) reduce)   ;do if so
 ({"empty seq" :error "Cannot find the maximum element of an empty sequence" :message} raise)  ;do if not
 if

) :seq-max</lang>

MiniScript

There is a list.max function in the listUtil module, but if you're working in an environment without this module or just want to write it yourself, you might do it as follows. <lang MiniScript>list.max = function()

   if not self then return null
   result = self[0]
   for item in self
       if item > result then result = item
   end for
   return result

end function

print [47, 11, 42, 102, 13].max</lang>

МК-61/52

<lang>П0 С/П x=0 07 ИП0 x<0 00 max БП 00</lang>

or

<lang>П0 ИП0 С/П - x<0 01 Вx П0 БП 01</lang>

Modula-3

Modula-3 provides a builtin MAX function, but it only works on two elements (or enumerations) but not arrays or sets.

We provide a generic Maximum implementation: <lang modula3>GENERIC INTERFACE Maximum(Elem);

EXCEPTION Empty;

PROCEDURE Max(READONLY a: ARRAY OF Elem.T): Elem.T RAISES {Empty};

END Maximum.</lang>

<lang modula3>GENERIC MODULE Maximum(Elem);

PROCEDURE Max(READONLY arr: ARRAY OF Elem.T): Elem.T RAISES {Empty} =

 VAR max := FIRST(Elem.T);
 BEGIN
   IF NUMBER(arr) = 0 THEN
     RAISE Empty;
   END;
   FOR i := FIRST(arr) TO LAST(arr) DO
     IF arr[i] > max THEN
       max := arr[i];
     END;
   END;
   RETURN max;
 END Max;

BEGIN END Maximum.</lang>

Elem can be instantiated to any type (any type that can be compared with the '>' function). For convenience Modula-3 provides interfaces/modules for the built in types, like Integer, Real, LongReal, etc, which contain type definitions as well as properties specific to the type.

To make a generic interface/module for a specific type, you must instantiate it: <lang modula3>INTERFACE RealMax = Maximum(Real) END RealMax.</lang> <lang modula3>MODULE RealMax = Maximum(Real) END RealMax.</lang>

Now we can import RealMax into our source and use the Max function: <lang modula3>MODULE Main;

IMPORT RealMax, IO, Fmt;

VAR realarr := ARRAY [1..5] OF REAL {1.1, 1.0, 0.0, 2.4, 3.3};

BEGIN

 IO.Put(Fmt.Real(RealMax.Max(realarr)) & "\n");

END Main.</lang>

MontiLang

MontiLang has a builtin statement MAX which finds the maximum of the top two items on the stack. By looping through an array and pushing to the stack, the largest item in an array can be found. <lang MontiLang>2 5 3 12 9 9 56 2 ARR

LEN VAR l . 0 VAR i . FOR l

   GET i SWAP
   i 1 + VAR i .

ENDFOR . STKLEN 1 - VAR st . FOR st

   MAX

ENDFOR PRINT</lang>

Another way to do it.

<lang MontiLang>2 5 3 12 9 9 56 2 ARR print LEN VAR l . 0 VAR i . 0 FOR l

   swap
   GET i rot max
   i 1 + VAR i .

ENDFOR |Greatest number in the list: | out . print |Press ENTER to exit | input clear</lang>

MUMPS

<lang MUMPS> MV(A,U)

;A is a list of values separated by the string U
NEW MAX,T,I
FOR I=1:1 SET T=$PIECE(A,U,I) QUIT:T=""  S MAX=$SELECT(($DATA(MAX)=0):T,(MAX<T):T,(MAX>=T):MAX)
QUIT MAX

</lang> Usage:

USER>SET V=","
 
USER>SET B="-1,-1000,1000,2.3E5,8A,""A"",F"
 
USER>W $$MV^ROSETTA(B,V)
2.3E5

Nanoquery

<lang Nanoquery>def max(list) if len(list) = 0 return null end

largest = list[0] for val in list if val > largest largest = val end end return largest end</lang>

Neko

<lang ActionScript>/**

greatest element from a list (Neko Array)
Tectonics:
  nekoc greatest-element.neko
  neko greatest-element
  • /

var greatest = function(list) {

 var max, element;
 var pos = 1;
 if $asize(list) > 0  max = list[0];
 while pos < $asize(list) {
   element = list[pos];
   if max < element  max = element;
   pos += 1;
 }
 return max;

}

$print(greatest($array(5, 1, 3, 5)), "\n"); $print(greatest($array("abc", "123", "zyx", "def")), "\n");</lang>

Output:
prompt$ nekoc greatest-element.neko
prompt$ neko ./greatest-element.n
5
zyx

Nemerle

<lang Nemerle>using System; using Nemerle.Collections; using System.Linq; using System.Console;

module SeqMax {

   SeqMax[T, U] (this seq : T) : U
     where T : Seq[U]
     where U : IComparable
   {
       $[s | s in seq].Fold(seq.First(), (x, y) => {if (x.CompareTo(y) > 0) x else y})
   }
   
   Main() : void
   {
       def numbers = [1, 12, 3, -5, 6, 23];
       def letters = ['s', 'p', 'a', 'm'];
       
       // using SeqMax() method (as task says to "create a function")
       WriteLine($"numbers.SeqMax() = $(numbers.SeqMax())");
       WriteLine($"letters.SeqMax() = $(letters.SeqMax())");
       
       // using the already available Max() method
       WriteLine($"numbers.Max() = $(numbers.Max())");
       WriteLine($"letters.Max() = $(letters.Max())")
   }

}</lang>

Output:
numbers.SeqMax() = 23
letters.SeqMax() = s
numbers.Max() = 23
letters.Max() = s

NetRexx

<lang NetRexx>/* NetRexx */

options replace format comments java crossref savelog symbols binary

rn = Random() maxElmts = 100 dlist = double[maxElmts] rlist = Rexx[maxElmts] loop r_ = 0 to maxElmts - 1

 nr = rn.nextGaussian * 100.0
 dlist[r_] = nr
 rlist[r_] = Rexx(nr)
 end r_

say 'Max double:' Rexx(getMax(dlist)).format(4, 9) say 'Max Rexx:' getMax(rlist).format(4, 9)

return

method getMax(dlist = double[]) public static binary returns double

 dmax = Double.MIN_VALUE
 loop n_ = 0 to dlist.length - 1
   if dlist[n_] > dmax then dmax = dlist[n_]
   end n_
 return dmax

method getMax(dlist = Rexx[]) public static binary returns Rexx

 dmax = Rexx(Double.MIN_VALUE)
 loop n_ = 0 to dlist.length - 1
   dmax = dlist[n_].max(dmax)
   end n_
 return dmax

</lang>

Output:
Max double:  274.457568703
Max   Rexx:  274.457568703

NewLISP

<lang NewLISP>(max 1 2 3 5 2 3 4)</lang>

<lang NEWLISP>(apply max '(1 2 3 5 2 3 4)) ; apply to list

Added by Nehal-Singhal 2018-05-29</lang>

Nial

The behavior of multi-dimensional arrays is like J

<lang nial>max 1 2 3 4 =4</lang>

Nim

<lang Nim>echo max([2,3,4,5,6,1])</lang>

Output:
6

Oberon-2

Works with: oo2c Version 2

<lang oberon2> MODULE GreatestElement1; IMPORT

 ADT:ArrayList,
 Object:Boxed,
 Out;
 

VAR

 a: ArrayList.ArrayList(Boxed.LongInt);
 max: Boxed.LongInt;
 
 
 PROCEDURE Max(al: ArrayList.ArrayList(Boxed.LongInt)): Boxed.LongInt;
 VAR
   i: LONGINT;
   item, max: Boxed.LongInt;
 BEGIN
   max := NEW(Boxed.LongInt,MIN(LONGINT));
   
   i := 0;
   WHILE (i < al.size) DO
     item := al.Get(i);
     IF item.value > max.value THEN max := item END;
     INC(i)  
   END;
   RETURN max
 END Max;
 

BEGIN

 a := NEW(ArrayList.ArrayList(Boxed.LongInt),5);
 a.Append(NEW(Boxed.LongInt,10));
 a.Append(NEW(Boxed.LongInt,32));
 a.Append(NEW(Boxed.LongInt,4));
 a.Append(NEW(Boxed.LongInt,43));
 a.Append(NEW(Boxed.LongInt,9));
 
 max := Max(a);
 Out.String("Max: ");Out.LongInt(max.value,4);Out.Ln

END GreatestElement1. </lang>

Simple version <lang oberon2> MODULE GreatestElement2; IMPORT

 Out;

VAR

 a: ARRAY 10 OF LONGINT;
 
 PROCEDURE Max(a: ARRAY OF LONGINT): LONGINT;
 VAR
   i, max: LONGINT;
 BEGIN
   max := MIN(LONGINT);
   FOR i := 0 TO LEN(a) - 1 DO
     IF a[i] > max THEN max := a[i] END;
   END;
   
   RETURN max
 END Max;

BEGIN

 a[0] := 10;
 a[1] := 32;
 a[2] := 4;
 a[3] := 43;
 a[4] := 9;
 
 Out.String("Max: ");Out.LongInt(Max(a),4);Out.Ln

END GreatestElement2. </lang>

Output:

(in both programs)

Max:   43

Objeck

The language has a "Max" method for vectors. <lang objeck> values := IntVector->New([4, 1, 42, 5]); values->Max()->PrintLine(); </lang>

Objective-C

Works with: GNUstep
Works with: Cocoa

This code "extends" (through Objective-C categories) the NSArray adding the method maximumValue; this one iterates over the objects of the collection calling the method compare, if it exists for the object of the collection. Since normally comparing makes sense between numbers, the code also check if the objects being compared are of "kind" NSNumber. If one eliminates this check (substituting it maybe with one that checks if the two object are of the same "kind"), the code is able to get a maximum value for any objects for which make sense a compare method (e.g. strings), that must be implemented.

If there's no a known way of comparing two objects of the collection (or if the objects are not "NSNumber"), the the method return nil (the void object).


<lang objc>#import <Foundation/Foundation.h>

@interface NSArray (WithMaximum) - (id)maximumValue; @end

@implementation NSArray (WithMaximum) - (id)maximumValue {

 if ( [self count] == 0 ) return nil;
 id maybeMax = self[0];
 for ( id el in self ) {
   if ( [maybeMax respondsToSelector: @selector(compare:)] &&

[el respondsToSelector: @selector(compare:)] && [el isKindOfClass: [NSNumber class]] && [maybeMax isKindOfClass: [NSNumber class]] ) {

     if ( [maybeMax compare: el] == NSOrderedAscending )

maybeMax = el;

   } else { return nil; }
 }
 return maybeMax;

} @end</lang>

This example mixes integers with a double value, just to show that everything is fine until they are NSNumber.

<lang objc>int main() {

 @autoreleasepool {
   NSArray *collection = @[@1, @2, @10, @5, @10.5];
 
   NSLog(@"%@", [collection maximumValue]);
 }
 return 0;

}</lang>

OCaml

<lang ocaml>let my_max = function

   [] -> invalid_arg "empty list"
 | x::xs -> List.fold_left max x xs</lang>
# my_max [4;3;5;9;2;3] ;;
- : int = 9

Octave

Octave's max accepts a vector (and can return also the index of the maximum value in the vector)

<lang octave>m = max( [1,2,3,20,10,9,8] );  % m = 20 [m, im] = max( [1,2,3,20,10,9,8] ); % im = 4</lang>

Oforth

<lang Oforth>[1, 2.3, 5.6, 1, 3, 4 ] reduce(#max)</lang>

Ol

Basics: <lang scheme>

builtin function

(max 1 2 3 4 5) ; 5

(define x '(1 2 3 4 5))

using to numbers list

(apply max x) ; 5

using list reducing

(fold max (car x) x) ; 5

manual lambda-comparator

(print (fold (lambda (a b)

  (if (less? a b) b a))
  (car x) x)) ; 5

</lang>

ooRexx

version

<lang ooRexx> -- routine that will work with any ordered collection or sets and bags containing numbers.

routine listMax
 use arg list 
 items list~makearray   -- since we're dealing with different collection types, reduce to an array
 if items~isEmpty then return .nil   -- return a failure indicator.  could also raise an error, if desired
 largest = items[1]


 -- note, this method does call max one extra time.  This could also use the 
 -- do i = 2 to items~size to avoid this
 do item over items 
    largest = max(item, largest) 
 end  
 return largest 

</lang>

version 2 works with any strings

<lang>/* REXX ***************************************************************

  • 30.07.2013 Walter Pachl as for REXX
                                                                                                                                            • /

s=.list~of('Walter','lives','in','Vienna') say listMax(s) -- routine that will work with any ordered collection or sets and bags.

routine listMax
 use arg list
 items=list~makearray   -- since we're dealing with different collection types, reduce to an array
 if items~isEmpty then return .nil   -- return a failure indicator.  could also raise an error, if desired
 largest = items[1]
 -- note, this method uses one extra comparison.  It could use
 -- do i = 2 to items~size to avoid this
 do item over items
    If item>>largest Then
      largest = item
 end
 return largest</lang>

OxygenBasic

<lang> 'Works on any list with element types which support '>' comparisons

macro max any(R, A, N, i)

================
 scope
   indexbase 1
   int i
   R=A(1)
   for i=2 to N
     if A(i)>R
       R=A(i)
     endif
   next
 end scope

end macro

'DEMO

=

redim double d(100) d={ 1.1, 1.2, 5.5, -0.1, -12.0 } double m=max(d,5) print "greatest element of d(): " m '5.5 </lang>

Oz

<lang oz>declare

 fun {Maximum X|Xr}         %% pattern-match on argument to make sure the list is not empty
    {FoldL Xr Value.max X}  %% fold the binary function Value.max over the list
 end

in

 {Show {Maximum [1 2 3 4 3]}}</lang>

PARI/GP

<lang parigp>vecmax(v)</lang>

Pascal

See Delphi

Works with: Free Pascal

or try this, for it shows the according position <lang pascal>program GElemLIst; {$IFNDEF FPC}

 {$Apptype Console}

{$else}

 {$Mode Delphi}

{$ENDIF}

uses

 sysutils;

const

 MaxCnt = 1000000;

type

  tMaxIntPos= record
                 mpMax,
                 mpPos : integer;
               end;
  tMaxfltPos= record
                 mpMax : double;
                 mpPos : integer;
               end;


function FindMaxInt(const ia: array of integer):tMaxIntPos; //delivers the highest Element and position of integer array var

 i  : NativeInt;
 tmp,max,ps: integer;

Begin

 max := -MaxInt-1;
 ps := -1;
 //i = index of last Element
 i := length(ia)-1;
 IF i>=0 then Begin
   max := ia[i];
   ps := i;
   dec(i);
   while i> 0 do begin
     tmp := ia[i];
     IF max< tmp then begin
       max := tmp;
       ps := i;
       end;
     dec(i);
     end;
   end;
 result.mpMax := Max;
 result.mpPos := ps;

end;

function FindMaxflt(const ia: array of double):tMaxfltPos; //delivers the highest Element and position of double array var

 i,
 ps: NativeInt;
 max : double;
 tmp : ^double;//for 32-bit version runs faster

Begin

 max := -MaxInt-1;
 ps := -1;
 //i = index of last Element
 i := length(ia)-1;
 IF i>=0 then Begin
   max := ia[i];
   ps := i;
   dec(i);
   tmp := @ia[i];
   while i> 0 do begin
     IF tmp^>max  then begin
       max := tmp^;
       ps := i;
       end;
     dec(i);
     dec(tmp);
     end;
   end;
 result.mpMax := Max;
 result.mpPos := ps;

end;

var

 IntArr : array of integer;
 fltArr : array of double;
 ErgInt : tMaxINtPos;
 ErgFlt : tMaxfltPos;
 i: NativeInt;

begin

 randomize;
 setlength(fltArr,MaxCnt); //filled with 0
 setlength(IntArr,MaxCnt); //filled with 0.0
 For i := High(fltArr) downto 0 do
   fltArr[i] := MaxCnt*random();
 For i := High(IntArr) downto 0 do
   IntArr[i] := round(fltArr[i]);
 ErgInt := FindMaxInt(IntArr);
 writeln('FindMaxInt ',ErgInt.mpMax,' @ ',ErgInt.mpPos);
 Ergflt := FindMaxflt(fltArr);
 writeln('FindMaxFlt ',Ergflt.mpMax:0:4,' @ ',Ergflt.mpPos);

end.</lang> Out, because of the searchdirection Position of FindMaxFlt is below FindMaxInt

FindMaxInt 999999 @ 691620
FindMaxFlt 999999.0265 @ 14824

Perl

<lang perl>sub max {

   my $max = shift;
   for (@_) { $max = $_ if $_ > $max }
   return $max;

}</lang>

It is already implemented in the module List::Util's max() function: <lang perl>use List::Util qw(max);

max(@values);</lang>

Phix

with javascript_semantics
?max({1,1234,62,234,12,34,6})
?max({"ant", "antelope", "dog", "cat", "cow", "wolf", "wolverine", "aardvark"})
Output:
1234
"wolverine"

Phixmonti

<lang Phixmonti>"1" "1234" "62" "234" "12" "34" "6" stklen tolist dup "Alphabetic order: " print max print nl

len for

   var i
   i get tonum i set

endfor "Numeric order: " print max print</lang>

With syntactic sugar

<lang Phixmonti>include ..\Utilitys.pmt

( "1" "1234" "62" "234" "12" "34" "6" )

dup "Alphabetic order: " print max print nl

def f

   tonum

enddef

getid f map

dup "Numeric order: " print max print </lang>

PHP

The built-in PHP function max() already does this. <lang php>max($values)</lang>

PicoLisp

<lang PicoLisp>: (max 2 4 1 3) # Return the maximal argument -> 4

(apply max (2 4 1 3)) # Apply to a list

-> 4

(maxi abs (2 -4 -1 3)) # Maximum according to given function

-> -4</lang>

PL/I

<lang pli> maximum = A(lbound(A,1)); do i = lbound(A,1)+1 to hbound(A,1);

 if maximum < A(i) then maximum = A(i);

end; </lang>

PostScript

Ghostscript has a max built-in:

Works with: Ghostscript

<lang postscript>/findmax {

 dup 0 get exch    % put the first element underneath the array
 {max} forall      % replace it by the respective larger value if necessary

} def</lang>

If not using Ghostscript this gets a bit longer:

<lang postscript>/findmax {

 dup 0 get exch    % put the first element underneath the array
 {
   dup             % duplicate the current item
   2 index         % duplicate the current maximum value
   gt              % if the current item is larger
   {exch} if       % swap the two items so the previous maximum is now the top of the stack
   pop             % remove it
 } forall

} def</lang>

Library: initlib

<lang postscript> [1 2 3 4 5 4 3 2 1] uncons exch {max} fold </lang>

PowerBASIC

<lang powerbasic>FUNCTION PBMAIN()

   DIM x AS LONG, y AS LONG, z AS LONG
   RANDOMIZE TIMER
   FOR x = 1 TO 10
       y = INT(RND * 10000)
       z = MAX(y, z)
   NEXT
   ? STR$(z) & " was the highest value"

END FUNCTION</lang>

Output:
 8104 was the highest value

PowerShell

The Measure-Object cmdlet in PowerShell already has this capability: <lang powershell>function Get-Maximum ($a) {

   return ($a | Measure-Object -Maximum).Maximum

}</lang>

Prolog

SWI-Prolog already knows max_list. <lang Prolog> ?- max_list([1, 2, 10, 3, 0, 7, 9, 5], M). M = 10.</lang>

can be implemented like this:

<lang Prolog>max_list(L, V) :- select(V, L, R), \+((member(X, R), X > V)). </lang>

PureBasic

<lang PureBasic>Procedure.f Max (Array a.f(1))

  Protected last, i, ret.f
  ret = a(0)   
  last = ArraySize(a())
  For i = 1 To last
     If ret < a(i)
        ret = a(i)
     EndIf
  Next
  
  ProcedureReturn ret

EndProcedure</lang>

PureBasic: another solution

<lang purebasic>Procedure.f maxelement(List tl.f())

 ForEach tl() : mx.f=mx*Bool(mx>=tl())+tl()*Bool(mx<tl()) : Next
 ProcedureReturn mx

EndProcedure

NewList testlist.f() : OpenConsole() For i=0 To 99 : AddElement(testlist()) : testlist()=Sqr(Random(1000)) : Next Print("Greatest element = "+StrF(maxelement(testlist()),8)) : Input()</lang>

Output:
Greatest element = 31.59113884

Python

The built-in Python function max() already does this. <lang python>max(values)</lang>

Of course this assumes we have a list or tuple (or other sequence like object). (One can even find the max() or min() character of a string since that's treated as a sequence of characters and there are "less than" and "greater than" operations (object methods) associate with those characters).

If we truly were receiving a stream of data then in Python, such streams are usually iterable, meaning they have a way of generating one item at a time from the stream.

max(), (and min()), can take iterables and a key argument which takes a function that can transform each item into a type that we can compare, for example, if the stream were returning string representations of integers, one to a line, you could do <lang python>>>> floatstrings = ['1\n', ' 2.3\n', '4.5e-1\n', '0.01e4\n', '-1.2'] >>> max(floatstrings, key = float) '0.01e4\n' >>></lang> Normally we would want the converted form as the maximum and we could just as easily write: <lang python>>>> max(float(x) for x in floatstrings) 100.0 >>></lang> Or you can write your own functional version, of the maximum function, using reduce and lambda <lang python>>>> mylist = [47, 11, 42, 102, 13] >>> reduce(lambda a,b: a if (a > b) else b, mylist) 102</lang>

Q

<lang Q>q)l:2 9 3 8 4 7 q)max l 9</lang>

Quackery

<lang Quackery>[ behead swap witheach max ] is [max] ( [ --> n )</lang>

R

<lang R>v <- c(1, 2, 100, 50, 0) print(max(v)) # 100</lang>

Racket

The "max" function it built in and takes an arbitrary amount of arguments. <lang racket>(max 12 9 8 17 1)</lang>

Output:
17

To use with a list, there is apply: <lang racket>(apply max '(12 9 8 17 1))</lang>

However, if you want to write the function yourself: <lang racket> (define (my-max l)

 (define (max-h l greatest)
   (cond [(empty? l) greatest]
         [(> (first l) greatest) (max-h (rest l) (first l))]
         [else (max-h (rest l) greatest)]))
 (if (empty? l) empty (max-h l (first l))))

</lang>

or with a "for" loop: <lang racket> (define (my-max l)

 (for/fold ([max #f]) ([x l])
   (if (and max (> max x)) max x)))

</lang>

Raku

(formerly Perl 6) The built-in function works with any type that defines ordering. <lang perl6>say max 10, 4, 5, -2, 11; say max <zero one two three four five six seven eight nine>;

  1. Even when the values and number of values aren't known until runtime

my @list = flat(0..9,'A'..'H').roll((^60).pick).rotor(4,:partial)».join.words; say @list, ': ', max @list; </lang>

Sample output:
11
zero
[6808 013C 6D5B 4219 29G9 DC13 CA4F 55F3 AA06 0AGF DAB0 2]: DC13

RapidQ

<lang vb>functioni FindMax(...) as double

   dim x as integer
       
   for x = 1 to ParamValCount
       IF ParamVal(x) > Result THEN Result = ParamVal(x)
   next

End functioni

Print FindMax(50, 20, 65, 20, 105) </lang>

Rascal

Rascal has a built-in function that gives the greatest element of a list <lang rascal> rascal>import List; ok

rascal>max([1,2,3,4]); int: 4 </lang>

Raven

<lang Raven>[ 1 2 3 4 ] max "%d\n" print</lang>

Output:
4

Randomly generated list size and elements <lang Raven>100 choose as $cnt [ ] as $lst 0 $cnt 1 range each drop 100 choose $lst push $lst print $lst max "max value: %d\n" print</lang>

REBOL

<lang REBOL>REBOL [

   Title: "Maximum Value"
   URL: http://rosettacode.org/wiki/Maximum_Value

]

max: func [ "Find maximum value in a list." values [series!] "List of values." ] [ first maximum-of values ]

print ["Max of" mold d: [5 4 3 2 1] "is" max d] print ["Max of" mold d: [-5 -4 -3 -2 -1] "is" max d]</lang>

Output:
Max of [5 4 3 2 1] is 5
Max of [-5 -4 -3 -2 -1] is -1

Red

<lang Red>Red [] list: [1 2 3 5 4] print last sort list </lang>

REXX

The numbers in the list may be any valid REXX number   (integer, negative, floating point, etc.)

using a list

<lang rexx>/*REXX program finds the greatest element in a list (of the first 25 reversed primes).*/ $ = reverse(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97) say 'list of numbers = ' $ /*show the original list of numbers. */ big=word($, 1) /*choose an initial biggest number. */

  1. = words($); do j=2 to # /*traipse through the list, find max. */
                    big=max(big, word($, j) )   /*use the  MAX  BIF to find the biggie.*/
                    end   /*j*/

say /*stick a fork in it, we're all done. */ say 'the biggest value in a list of ' # " numbers is: " big</lang>

output   when using the default (internal) input:
list of numbers =  79 98 38 97 37 17 76 16 95 35 74 34 14 73 13 92 32 91 71 31 11 7 5 3 2

the biggest value in a list of  25  numbers is:  98

using an array

<lang rexx>/*REXX program finds the greatest element in a list (of the first 25 reversed primes).*/ @.=; @.1 = 2; @.2 = 3; @.3 = 5; @.4 = 7; @.5 =11; @.6 =31; @.7 =71

          @.8 =91;    @.9 =32;    @.10=92;    @.11=13;    @.12=73;    @.13=14;    @.14=34
          @.15=74;    @.16=35;    @.17=95;    @.18=16;    @.19=76;    @.20=17;    @.21=37
          @.22=97;    @.23=38;    @.24=98;    @.25=79

big=@.1 /*choose an initial biggest number. */

               do #=2  while @.#\==           /*traipse through whole array of nums. */
               big = max(big, @.#)              /*use a BIF to find the biggest number.*/
               end   /*#*/ 
                                                /*stick a fork in it,  we're all done. */

say 'the biggest value in an array of ' #-1 " elements is: " big</lang>

output   when using the default (internal) input:
the biggest value in an array of  25  elements is:  98

using a list from the terminal

<lang rexx>/*REXX program finds the greatest element in a list of numbers entered at the terminal*/ say '────────────────── Please enter a list of numbers (separated by blanks or commas):' parse pull $; #=words($) /*get a list of numbers from terminal. */ $=translate($, , ',') /*change all commas (,) to blanks. */ big=word($,1); do j=2 to # /*traipse through the list of numbers. */

                       big=max(big, word($,j))  /*use a BIF for finding the max number.*/
                       end   /*j*/

say /*stick a fork in it, we're all done. */ say '────────────────── The biggest value in the list of ' # " elements is: " big</lang> Programming note:   the   max   BIF normalizes the number returned (eliding the leading superfluous zeroes).

If this is undesirable, then the   do   loop (shown above) can be replaced with: <lang rexx>···; do j=2 to #; _=word($,j)

                       if _>big  then big=_
                       end   /*j*/</lang>
output   when using the input from the terminal via a user prompt:
──────────────────  Please enter a list of numbers  (separated by blanks or commas):
-12  -3  0  1.1  1e2  99.2  00245                   ◄■■■■■■■■■■ user input 

────────────────── The biggest value in the list of  7  elements is:  245

list of any strings

<lang rexx>/* REXX ***************************************************************

  • If the list contains any character strings, the following will work
  • Note the use of >> (instead of >) to avoid numeric comparison
  • Note that max() overrides the builtin function MAX
  • 30.07.2013 Walter Pachl
                                                                                                                                            • /

list='Walter Pachl living in Vienna' Say max(list) list='8 33 -12' Say max(list) Exit max: Procedure Parse Arg l max=word(l,1) Do i=2 To words(l)

 If word(l,i)>>max Then
   max=word(l,i)
 End

Return max</lang>

output   when using an   ASCII   system:
living
8    
output   when using an   EBCDIC   system:
Walter
8    

output when using a list which is:   12 111111 1 (lexigraphically 12 is greater than 111111)

12

Ring

<lang ring>aList = [1,2,4,5,10,6,7,8,9] see max(aList)</lang>

Output:
10

Ruby

max is a method of all Enumerables <lang ruby>values.max</lang>

Run BASIC

<lang Runbasic>list$= "1 12 -55 46 41 3.66 19" while word$(list$,i+1," ") <> ""

 mx = max(mx,val(word$(list$,i+1," ")))
 i = i + 1

wend print mx</lang>

Rust

This is built in functionality for everything that can be iterated over. It returns an Option<T>, meaning Some(e) if there are elements in the iterator and None if it is empty. <lang rust>fn main() {

   let nums = [1,2,39,34,20];
   println!("{:?}", nums.iter().max());
   println!("{}", nums.iter().max().unwrap());

}</lang>

Output:
Some(39)
39

S-lang

Starting w/an array, this is trivial: <lang S-lang>variable a = [5, -2, 0, 4, 666, 7]; print(max(a));</lang>

output: 666

If a is a list instead of an array, then: <lang S-lang>a = {5, -2, 0, 4, 666, 7}; print(max(list_to_array(a)));</lang>

Scala

Library: Scala

<lang Scala>def noSweat(list: Int*) = list.max // Test assert(noSweat(1, 3, 12, 7) == 12)</lang>

Scheme

The built-in Scheme function max takes the max of all its arguments. <lang scheme>(max 1 2 3 4) (apply max values) ; find max of a list</lang>

Seed7

<lang seed7>$ include "seed7_05.s7i";

const func integer: max (in array integer: values) is func

 result
   var integer: max is 0;
 local
   var integer: index is 0;
 begin
   max := values[1];
   for index range 2 to length(values) do
     if values[index] > max then
       max := values[index];
     end if;
   end for;
 end func;

const proc: main is func

 begin
   writeln(max([] (1, 2, 6, 4, 3)));
 end func;</lang>
Output:
6

Self

Using reduceWith: it is very simple to find the maximum value among a collection.

<lang self>(1 & 2 & 3 & 4 & 20 & 10 & 9 & 8) asVector reduceWith: [:a :b | a max: b] "returns 20"</lang>

Or, since it's "built-in", you can simply do:

<lang self>(1 & 2 & 3 & 4 & 20 & 10 & 9 & 8) asVector max. "returns 20"</lang>

SenseTalk

<lang sensetalk>put the max of (1, 5, 666, -1000, 3) put the highest value of [88,-2,6,55,103,0]</lang> Output: <lang sensetalk>666 103</lang>

Sidef

max method returns the greatest element in a list. It works only if the array's elements have the same type (e.g.: strings, numbers). <lang ruby>values.max;</lang>

Slate

<lang slate>#(1 2 3 4 20 10 9 8) reduce: [| :a :b | a max: b]</lang>

Smalltalk

Using fold it is very simple to find the maximum value among a collection.

<lang smalltalk>#(1 2 3 4 20 10 9 8) fold: [:a :b | a max: b] "returns 20"</lang>

Or, since it's "built-in", you can simply do:

Works with: Pharo version 1.4
Works with: Smalltalk/X

<lang smalltalk>#(1 2 3 4 20 10 9 8) max. "returns 20"</lang>

using #inject:into: <lang smalltalk> | list | list := #(1 2 3 4 20 10 9 8). list inject: (list at: 1) into: [ :number :each |

 number max: each ]

</lang>

SNOBOL4

<lang snobol4>while a = trim(input) :f(stop)

       max = gt(a,max) a   :(while)

stop output = max end</lang>

Standard ML

Comparisons are specific for each type. Here is a max function for a list of ints: <lang sml>fun max_of_ints [] = raise Empty

 | max_of_ints (x::xs) = foldl Int.max x xs</lang>
- max_of_ints [4,3,5,9,2,3];
val it = 9 : int

Stata

Use the summarize command to compute the maximum value of a variable:

<lang stata>qui sum x di r(max)</lang>

Mata has also several functions to compute minimum or maximum of a vactor or matrix:

<lang stata>a = 1,5,3,4,2,7,9,8 max(a)</lang>

Swift

Works with: Swift version 2.x

<lang swift>if let x = [4,3,5,9,2,3].maxElement() {

 print(x) // prints 9

}</lang>

Works with: Swift version 1.x

<lang swift>let x = maxElement([4,3,5,9,2,3]) println(x) // prints 9</lang>

Tailspin

Simplest is to use the built-in collector <lang tailspin> [1, 5, 20, 3, 9, 7] ... -> ..=Max&{by: :(), select: :()} -> !OUT::write // outputs 20

// This is how the Max-collector is implemented in the standard library: processor Max&{by:, select:}

 @:[];
 sink accumulate
   <?($@Max <=[]>)
     | ?($(by) <$@Max.max..>)>  @Max: {max: $(by), result: $(select)};
 end accumulate
 source result
   $@Max.result !
 end result

end Max </lang> But here is how to find the max more manually/programmatically, it is pretty easy as well <lang tailspin> templates max

 @: $(1);
 $(2..last)... -> #
 $@!
 when <$@..> do @: $;

end max

[1, 5, 20, 3, 9, 7] -> max -> !OUT::write // outputs 20 </lang> Can also be written as an inline templates <lang tailspin> [1, 5, 20, 3, 9, 7] -> \(@: $(1); $(2..last)... -> # $@ ! when <$@..> do @: $;\) -> !OUT::write // outputs 20 </lang> Or we can do just the matching in an inline templates referencing the outer state <lang tailspin> templates max

 @: $(1);
 $(2..last)... -> \(<$@max..> @max: $;\) -> !VOID
 $@!

end max

[1, 5, 20, 3, 9, 7] -> max -> !OUT::write // outputs 20 </lang>

Tcl

Works with: Tcl version 8.5

Use the {*} expansion operator to substitute the list value with its constituent elements <lang tcl>package require Tcl 8.5

set values {4 3 2 7 8 9}

tcl::mathfunc::max {*}$values ;# ==> 9</lang>

TI-83 BASIC

The builtin max function can be applied to lists. max({1, 3, 2.

TI-89 BASIC

The builtin max function can be applied to lists. max({1, 3, 2}) = 3.

Transd

<lang scheme>#lang transd

MainModule: {

   _start: (λ 
       (textout (max 9 6 2 11 3 4) " ")
       (with v [1, 45, 7, 274, -2, 34]
           (textout (max-element v) " ") 
           (textout (max-element-idx v))
   ))

}</lang>

Output:
11 274 3

Trith

<lang trith>[1 -2 3.1415 0 42 7] [max] foldl1</lang>

TUSCRIPT

<lang tuscript> $$ MODE TUSCRIPT LOOP n,list="2'4'0'3'1'2'-12" IF (n==1) greatest=VALUE(list) IF (list>greatest) greatest=VALUE(list) ENDLOOP PRINT greatest </lang>

Output:
4

uBasic/4tH

Since uBasic/4tH has a stack, it's only logical to use it. <lang>Push 13, 0, -6, 2, 37, -10, 12 ' Push values on the stack Print "Maximum value = " ; FUNC(_FNmax(7)) End ' We pushed seven values

_FNmax Param(1)

 Local(3)
 d@ = -(2^31)                         ' Set maximum to a tiny value
 For b@ = 1 To a@                     ' Get all values from the stack
   c@ = Pop()
   If c@ > d@ THEN d@ = c@            ' Change maximum if required
 Next

Return (d@) ' Return the maximum</lang>

UNIX Shell

Works with: bash version 3
Works with: pdksh

<lang bash>max() {

 local m=$1
 shift
 while [ $# -gt 0 ]
 do
   [ "$m" -lt "$1" ] && m=$1
   shift
 done
 echo "$m"

}

max 10 9 11 57 1 12</lang>

Works with: Bourne Shell

<lang bash>max() {

 m=$1  # Bourne Shell has no local command.
 shift
 while [ $# -gt 0 ]
 do
   [ "$m" -lt "$1" ] && m=$1
   shift
 done
 echo "$m"

}</lang>

Ursa

The max function: <lang ursa>def max (int<> list) decl int max i set max list<0>

for (set i 1) (< i (- (size list) 1)) (inc i) if (> list max) set max list end if end for

return max end max</lang>

In use: (assuming the function is in the file max.u)

cygnus/x ursa v0.75 (default, release 0)
[Oracle Corporation JVM 1.8.0_51 on Mac OS X 10.10.5 x86_64]
> import "max.u"
> decl int<> list
> append 5 1 7 3 9 2 list
> out (max list) endl console
9
> _

Ursala

The built-in $^ operator takes a binary predicate of any type to a function extracting the maximum value from a non-empty list of that type. In this case it is used with fleq, the partial order relation on floating point numbers. <lang Ursala>#import flo

  1. cast %e

example = fleq$^ <-1.,-2.,0.,5.,4.,6.,1.,-5.></lang>

Output:
6.000000e+00

V

Assuming it is a list of positive numbers <lang v>[4 3 2 7 8 9] 0 [max] fold =9</lang>

If it is not <lang v>[4 3 2 7 8 9] dup first [max] fold</lang>

=9

VBA

<lang vb>Option Explicit

Sub Main() Dim a

  a = Array(1, 15, 19, 25, 13, 0, -125, 9)
  Debug.Print Max_VBA(a)

End Sub

Function Max_VBA(Arr As Variant) As Long Dim i As Long, temp As Long

  temp = Arr(LBound(Arr))
  For i = LBound(Arr) + 1 To UBound(Arr)
     If Arr(i) > temp Then temp = Arr(i)
  Next i
  Max_VBA = temp

End Function</lang>

Output:
25

VBScript

<lang vb> Function greatest_element(arr) tmp_num = 0 For i = 0 To UBound(arr) If i = 0 Then tmp_num = arr(i) ElseIf arr(i) > tmp_num Then tmp_num = arr(i) End If Next greatest_element = tmp_num End Function

WScript.Echo greatest_element(Array(1,2,3,44,5,6,8)) </lang>

Output:
44

Vim Script

for numbers (not floats): <lang vim>max([1, 3, 2])</lang> result: 3

for strings (with configurable ignore-case): <lang vim>function! Max(list, ...)

   " {list}	list of strings
   " {a:1}	'i': ignore case, 'I': match case, otherwise use 'ignorecase' option
   if empty(a:list)

return 0

   endif
   let gt_op = a:0>=1 ? get({'i': '>?', 'I': '>#'}, a:1, '>') : '>'
   let cmp_expr = printf('a:list[idx] %s maxval', gt_op)
   let maxval = a:list[0]
   let len = len(a:list)
   let idx = 1
   while idx < len

if eval(cmp_expr) let maxval = a:list[idx] endif let idx += 1

   endwhile
   return maxval

endfunction</lang>

Visual Basic

<lang vb>Public Function ListMax(anArray())

   'return the greatest element in array anArray
   'use LBound and UBound to find its length
   n0 = LBound(anArray)
   n = UBound(anArray)
   theMax = anArray(n0)
   For i = (n0 + 1) To n
       If anArray(i) > theMax Then theMax = anArray(i)
   Next
   ListMax = theMax

End Function


Public Sub ListMaxTest()

   Dim b()
   'test function ListMax
   'fill array b with some numbers:
   b = Array(5992424433449#, 4534344439984#, 551344678, 99800000#)
   'print the greatest element
   Debug.Print "Greatest element is"; ListMax(b())

End Sub</lang>

Result:

ListMaxTest
Greatest element is 5992424433449

Wart

Wart defines max in terms of the more general best. <lang python>def (best f seq)

 if seq
   ret winner car.seq
     each elem cdr.seq
       if (f elem winner)
         winner <- elem

def (max ... args)

 (best (>) args)</lang>

(>) is > while suppressing infix expansion.

WDTE

<lang wdte>let s => import 'stream'; let a => import 'arrays';

let max list =>

 a.stream list
 -> s.extent 1 >
 -> at 0
 ;</lang>

extent is a standard library function that returns a sorted list of the elements of a stream that fit the given function best, so > results in the maximum element.

Wortel

The @maxl returns the maximum value of a list: <lang wortel>@maxl [1 6 4 6 4 8 6 3] ; returns 8</lang>

Wren

<lang ecmascript>var max = Fn.new { |a| a.reduce { |m, x| (x > m) ? x : m } }

var a = [42, 7, -5, 11.7, 58, 22.31, 59, -18] System.print(max.call(a))</lang>

Output:
59

XPL0

The set of values is the lengths of the lines of text in the input file.

<lang XPL0>include c:\cxpl\codes; \include 'code' declarations

def Tab=$09, LF=$0A, CR=$0D, EOF=$1A;

int CpuReg, Hand; char CmdTail($80); int I, Max, C;

[\Copy file name on command line, which is in the Program Segment Prefix (PSP) \ ES=CpuReg(11), to the CmdTail array, which is in our Data Segment = CpuReg(12) CpuReg:= GetReg; \point to copy of CPU registers Blit(CpuReg(11), $81, CpuReg(12), CmdTail, $7F); Hand:= FOpen(CmdTail, 0); \open file for input and get its handle FSet(Hand, ^I); \assign handle to device 3 OpenI(3); \initialize file for input

Max:= 0; \scan file for longest line repeat I:= 0;

       repeat  C:= ChIn(3);
               case C of
                 CR, LF, EOF:  [];     \don't count these characters
                 Tab:  [I:= I+8 & ~7]  \(every 8th column)
               other   I:= I+1;        \count all other characters
       until   C=LF or C=EOF;
       if I > Max then Max:= I;

until C = EOF; Text(0, "Longest line = "); IntOut(0, Max); CrLf(0); ]</lang>

Example of running the program on its source code:

maxline maxline.xpl
Longest line = 80

XSLT

The desired value is the first in a sequence that has been sorted numerically in descending order. <lang xml><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text"/>

<xsl:template match="/"> <xsl:for-each select="/*/*"> <xsl:sort data-type="number" order="descending"/> <xsl:if test="position() = 1"> <xsl:value-of select="."/> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet></lang>

Sample input:

<lang xml><numbers> <number>3</number> <number>1</number> <number>12</number> <number>7</number> </numbers></lang>

Output:
 12

Yabasic

<lang Yabasic>l$ = "1,1234,62,234,12,34,6"

dim n$(1)

n = token(l$, n$(), ", ")

for i = 1 to n t$ = n$(i) if t$ > m$ then m$ = t$ end if // or: if t$ > m$ m$ = t$ if val(t$) > m then m = val(t$) end if // or: if val(t$) > m m = val(t$) next

print "Alphabetic order: ", m$, ", numeric order: ", m</lang>

Yacas

Input: <lang Yacas>Max({1, 3, 3, 7}) Max({Pi,Exp(1)+2/5,17*Cos(6)/5,Sqrt(91/10)}) Max({1,6,Infinity}) Max({})</lang>

Output:
 7
(17*Cos(2*Pi-6))/5
Infinity
Undefined

Yorick

The built-in function max does this. Interactive example:

> foo = [4, 3, 2, 7, 8, 9]
> max(foo)
9

zkl

<lang zkl>(1).max(1,2,3) //-->3 (66).max(1,2,3.14) //-->66</lang> If given a list, the max of the list is returned. The number/object just selects the method to call. Notice the difference between Int.max and Float.max. <lang zkl>(66).max(T(1,2,3)) //-->3 (66).max(T(1,2,3.14)) //-->3 (6.6).max(T(1,2,3.14)) //-->3.14</lang> For other object types, you could use: <lang zkl>fcn max{ vm.arglist.reduce(fcn(p,n){ if(p < n) n else p }) }</lang>

max(2,1,-40,50,2,4,2) //-->50
max(2) //-->2
max("foo","bar") //-->"foo"
max("3",4,"5") //-->"5" only if strings contain only digits

Zoea

<lang Zoea> program: max

 case: 1
       input: [7,3,5,9,2,6] 
       output: 9 
 case: 2 
       input: [1,5,3,2,7] 
       output: 7 

</lang>

ZX Spectrum Basic

<lang zxbasic>10 PRINT "Values" 20 LET z=0 30 FOR x=1 TO INT (RND*10)+1 40 LET y=RND*10-5 50 PRINT y 60 LET z=(y AND y>z)+(z AND y<z) 70 NEXT x 80 PRINT '"Max. value = ";z</lang>