Contents [hide]
Output
3
Code examples
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> static int maxValue( const int * array, int size) { int i; int max; max = array[0]; for (i = 1; i < size/ sizeof (array[0]); ++i) { if (max < array[i]) { max = array[i]; } } return max; } int main( void ) { const int array[] = {3, 1, 2}; printf ( "%i\n" , maxValue(array, sizeof (array)) ); } |
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; int main() { int array []= {3, 1, 2}; int largest = array[0] ; for ( int i=1; i < sizeof (array)/ sizeof (array[0]); ++i ) { if ( array[i] > largest ) { largest = array[i] ; } } cout << largest << '\n' ; return 0; } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Max { public static void main(String[] args) { int [] myList = { 3 , 1 , 2 }; int max = getMax(myList); System.out.println(max); } public static int getMax( int [] inputArray){ int maxValue = inputArray[ 0 ]; for ( int i= 1 ;i<inputArray.length;i++){ if (inputArray[i] > maxValue){ maxValue = inputArray[i]; } } return maxValue; } } |
JavaScript
1 2 | let array = [3, 1, 2] console.log(Math.max(...array)) |
Perl
1 2 3 | use List::Util 'max' ; my @array = (3, 1, 2); print max( @array ); |
PHP
1 2 3 | <?php $array = [3, 1, 2]; print (max( $array )); |
Python
1 2 | array = [ 3 , 1 , 2 ] print ( max (array)) |
Ruby
1 2 | array = [ 3 , 1 , 2 ] print(array.max) |
Shell
1 2 3 4 5 6 7 8 9 10 | #!/bin/sh array=(3 1 2) max=${array[0]} for i in ${array[@]} do if [ ${i} -gt $max ]; then max=${i} fi done echo $max; |

Author: Hajime Kurita, CEO/CTO of 1st Class, inc. Search Engineer & Web master
Certificate: AWS SOL Architect, Professional Scrum Master I, Class I IT Engineer(JP national certificate), TOEIC900+, Graduate of Tokyo university
Please follow My English twitter account for updates
IT Relatd service: VPS Ranking