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
233
votes
4 answers

What is the maximum float in Python?

I think the maximum integer in python is available by calling sys.maxint. What is the maximum float or long in Python? See also: Maximum and Minimum values for ints.
ladyfafa
  • 6,629
  • 7
  • 26
  • 22
221
votes
15 answers

CSS Div stretch 100% page height

I have a navigation bar on the left hand side of my page, and I want it to stretch to 100% of the page height. Not just the height of the viewport, but including the areas hidden until you scroll. I don't want to use javascript to accomplish…
Tom
  • 4,341
  • 9
  • 27
  • 21
221
votes
32 answers

How might I find the largest number contained in a JavaScript array?

I have a simple JavaScript Array object containing a few numbers. [267, 306, 108] Is there a function that would find the largest number in this array?
dotty
  • 40,405
  • 66
  • 150
  • 195
217
votes
18 answers

How to find all positions of the maximum value in a list?

I have a list: a = [32, 37, 28, 30, 37, 25, 27, 24, 35, 55, 23, 31, 55, 21, 40, 18, 50, 35, 41, 49, 37, 19, 40, 41, 31] max element is 55 (two elements on position 9 and 12) I need to find on which position(s) the maximum value is…
Bob
  • 10,427
  • 24
  • 63
  • 71
214
votes
4 answers

numpy max vs amax vs maximum

numpy has three different functions which seem like they can be used for the same things --- except that numpy.maximum can only be used element-wise, while numpy.max and numpy.amax can be used on particular axes, or all elements. Why is there more…
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
209
votes
10 answers

How can I get the maximum or minimum value in a vector?

How can I get the maximum or minimum value in a vector in C++? And am I wrong in assuming it would be more or less the same with an array? I need an iterator, right? I tried it with max_element, but I kept getting an…
bob blob
  • 2,191
  • 3
  • 15
  • 6
203
votes
5 answers

Find the column name which has the maximum value for each row

I have a DataFrame like this one: Communications and Search Business General Lifestyle 0 0.745763 0.050847 0.118644 0.084746 0 0.333333 0.000000 0.583333 0.083333 0 0.617021 0.042553 0.297872 0.042553 0 …
markov zain
  • 11,987
  • 13
  • 35
  • 39
188
votes
12 answers

Find maximum value of a column and return the corresponding row values using Pandas

Using Python Pandas I am trying to find the Country & Place with the maximum value. This returns the maximum value: data.groupby(['Country','Place'])['Value'].max() But how do I get the corresponding Country and Place name?
richie
  • 17,568
  • 19
  • 51
  • 70
187
votes
14 answers

Return index of greatest value in an array

I have this: var arr = [0, 21, 22, 7]; What's the best way to return the index of the highest value into another variable?
Stephen
  • 1,961
  • 3
  • 14
  • 11
181
votes
5 answers

Minimum and maximum date

I was wondering which is the minimum and the maximum date allowed for a Javascript Date object. I found that the minimum date is something like 200000 B.C., but I couldn't get any reference about it. Does anyone know the answer? I just hope that it…
MaxArt
  • 22,200
  • 10
  • 82
  • 81
160
votes
14 answers

Min/Max of dates in an array?

How can I find out the min and the max date from an array of dates? Currently, I am creating an array like this: var dates = []; dates.push(new Date("2011/06/25")) dates.push(new Date("2011/06/26")) dates.push(new Date("2011/06/27")) dates.push(new…
Legend
  • 113,822
  • 119
  • 272
  • 400
155
votes
10 answers

Maximum Year in Expiry Date of Credit Card

Various online services have different values for maximum year of expiry, when it comes to Credit Cards. For instance: Basecamp: +15 years (2025) Amazon: +20 years (2030) Paypal: +19 years (2029) What is the reasonable maximum here? Are there any…
Art
  • 23,747
  • 29
  • 89
  • 101
154
votes
12 answers

SQL Server: SELECT only the rows with MAX(DATE)

I have a table of data (the db is MSSQL): ID OrderNO PartCode Quantity DateEntered 417 2144 44917 100 18-08-11 418 7235 11762 5 18-08-11 419 9999 60657 100 18-08-11 420 9999 60657 90 …
GEMI
  • 2,239
  • 3
  • 20
  • 28
152
votes
5 answers

How to get the index of a maximum element in a NumPy array along one axis

I have a 2 dimensional NumPy array. I know how to get the maximum values over axes: >>> a = array([[1,2,3],[4,3,1]]) >>> amax(a,axis=0) array([4, 3, 3]) How can I get the indices of the maximum elements? I would like as output array([1,1,0])…
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
148
votes
12 answers

How do I find the maximum (larger, greater) of 2 numbers?

I have two variables value and run: value = -9999 run = problem.getscore() How can I find out which one is greater, and get the greater value? See also Find the greatest (largest, maximum) number in a list of numbers - those approaches work (and…
Shilpa
  • 1,815
  • 3
  • 19
  • 23