Maximum value. Largest, biggest, greatest.
max
max()
is a math library function available in many programming environments which returns the entity of greatest value from two or more candidate values.
Do not use max for questions about the maximal value of a type, e.g. Long.MAX_VALUE
, Integer.MAX_VALUE
, etc.
Examples
SQL
SQL MAX()
function is one of aggregate functions. It returns the largest value of column.
SELECT MAX(column_name) FROM table_name;
Python
In Python, the native max
function identifies the largest element of a set (e.g. a list).
>> max([1, 2, 3])
3
Java
In Java we can use java.lang.Math
class to compute maximum value of two arguments.
int maxElement = Math.max(x, y);
To compute maximum element in collection we can use, max()
method from java.util.Collections
class.
int maxElement = Collections.max(list);
Ruby
In Ruby to compute maximum element of collection we simply use max
function.
[4,7].max
Clojure
In Clojure we use max
function in the following way.
(max 1 2 3 4)
In above code max
is the function name and numbers 1..4
are arguments passed to it. We can also apply the max
function to lists.
(apply max [1 2 3 4])
R
In R we have function max
.
x <- c(1, 2, 3)
y <- c(4, 5)
z <- c(6, NA)
max(x)
max(y)
max(z)
max(z, na.rm = TRUE)
max(x, y, z, na.rm = TRUE) ## as same as max(c(x, y, z), na.rm = TRUE)
Rust
In Rust we can use std::cmp::max
to compute the maximum value of two arguments.
let max = std::cmp::max(x, y);
We can compute the maximum element in a collection by transforming it into an Iterator
and then calling the max
method on the iterator.
let max = collection.into_iter().max();