Questions tagged [max]

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 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();
8982 questions
55
votes
4 answers

How do I find the largest value in a column in postgres sql?

For example: name | weight jon 100 jane 120 joe 130 How do I only return the name of the person with the largest weight?
diesel
55
votes
15 answers

Excel CSV. file with more than 1,048,576 rows of data

I have been given a CSV file with more than the MAX Excel can handle, and I really need to be able to see all the data. I understand and have tried the method of "splitting" it, but it doesnt work. Some background: The CSV file is an Excel CSV file,…
Ostrich_Cloud
  • 551
  • 1
  • 4
  • 3
51
votes
7 answers

Java - limit number between min and max

I want to return the number as long as it falls within a limit, else return the maximum or minimum value of the limit. I can do this with a combination of Math.min and Math.max. public int limit(int value) { return Math.max(0, Math.min(value,…
Sean Connolly
  • 5,692
  • 7
  • 37
  • 74
50
votes
4 answers

How to extract the row with min or max values?

With a dataframe like this one: ID Year Temp ph 1 P1 1996 11.3 6.80 2 P1 1996 9.7 6.90 3 P1 1997 9.8 7.10 ... 2000 P2 1997 10.5 6.90 2001 P2 1997 9.9 7.00 2002 P2 1997 …
matteo
  • 4,683
  • 9
  • 41
  • 77
50
votes
1 answer

How to atomically update a maximum value?

In serial code, updating a maximum could be accomplished simply by template void update_maximum(T& maximum_value, T const& value) noexcept { if(value > maximum_value) maximum_value = value; } However, how should this be done for an…
Walter
  • 44,150
  • 20
  • 113
  • 196
47
votes
12 answers

Find the max of 3 numbers in Java with different data types

Say I have the following three constants: final static int MY_INT1 = 25; final static int MY_INT2 = -10; final static double MY_DOUBLE1 = 15.5; I want to take the three of them and use Math.max() to find the max of the three but if I pass in more…
Drew Bartlett
  • 1,863
  • 5
  • 23
  • 37
47
votes
8 answers

How to calculate the maximum of two numbers in Oracle SQL select?

This should be simple and shows my SQL ignorance: SQL> select max(1,2) from dual; select max(1,2) from dual * ERROR at line 1: ORA-00909: invalid number of arguments I know max is normally used for aggregates. What can I use here? In the…
Peter G.
  • 14,786
  • 7
  • 57
  • 75
46
votes
5 answers

Finding max value in the second column of a nested list?

I have a list like this: alkaline_earth_values = [['beryllium', 4], ['magnesium', 12], ['calcium', 20], ['strontium', 38], ['barium', 56], …
davelupt
  • 1,845
  • 4
  • 21
  • 32
46
votes
3 answers

Manage empty list/invalid input when finding max/min value of list (Python)

I'm finding max value and min value of a list by using max(list) and min(list) in Python. However, I wonder how to manage empty lists. For example if the list is an empty list [], the program raises 'ValueError: min() arg is an empty sequence' but…
Jon_Computer
  • 689
  • 2
  • 6
  • 9
46
votes
6 answers

Javascript max() function for 3 numbers

I need to find the highest number from 3 different numbers. The only thing I've found is max() but you can only use 2 numbers. Whats the best way?
Ben Shelock
  • 20,154
  • 26
  • 92
  • 125
45
votes
8 answers

MAX function in where clause mysql

How can I use max() function in where clause of a mysql query, I am trying: select firstName,Lastname,MAX(id) as max where id=max; this is giving me an error: Unknown column 'max' in 'where clause'
user3176971
  • 599
  • 2
  • 6
  • 16
45
votes
3 answers

SQL not a single-group group function

When I run the following SQL statement: SELECT MAX(SUM(TIME)) FROM downloads GROUP BY SSN It returns the maximum sum value of downloads by a customer, however if I try to find the social security number that that max value belongs to by adding it…
Tomek
  • 4,689
  • 15
  • 44
  • 52
45
votes
4 answers

numpy.max or max ? Which one is faster?

In python, which one is faster ? numpy.max(), numpy.min() or max(), min() My list/array length varies from 2 to 600. Which one should I use to save some run time ?
Froyo
  • 17,947
  • 8
  • 45
  • 73
44
votes
7 answers

How can I find the maximum value and its index in array in MATLAB?

Suppose I have an array, a = [2 5 4 7]. What is the function returning the maximum value and its index? For example, in my case that function should return 7 as the maximum value and 4 as the index.
Yuseferi
  • 7,931
  • 11
  • 67
  • 103
43
votes
10 answers

Find the smallest amongst 3 numbers in C++

Is there any way to make this function more elegant? I'm new to C++, I don't know if there is a more standardized way to do this. Can this be turned into a loop so the number of variables isn't restricted as with my code? float smallest(int x, int…
user1145538
  • 641
  • 2
  • 9
  • 14