Get maximum value in the array

C

#include <stdio.h>

static int maxValue(const int* array) {
    int i;
    int max;
    max = array[0];
    for (i = 1; i < sizeof(array)/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) );
}

C++

#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

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(node.js)

let array = [3, 1, 2]
console.log(Math.max(...array))

Perl

use List::Util 'max';
my @array = (3, 1, 2);
print max(@array);

PHP

<?php
$array = [3, 1, 2];
print(max($array));

Python 3

array = [3, 1, 2]
print(max(array))

Ruby

array = [3, 1, 2]
print(array.max)