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

Is there a vectorized parallel max() and min()?

I have a data.frame with columns "a" and "b". I want to add columns called "high" and "low" that contain the highest and the lowest among columns a and b. Is there a way of doing this without looping over the lines in the dataframe? edit: this is…
Generic Person
  • 735
  • 1
  • 6
  • 9
43
votes
7 answers

How to increase the value of a number to the next multiple of 10, 100, 1000, 10,000 and so on

You'll have to forgive the phrasing of this question, I'm sure there's a better, more succinct way to ask it, but I don't know it. Let's say I have a graph, and all the y-axis values are [0,4,5,3,2,5,6] The maximum value is six. So I would like the…
gargantuan
  • 8,888
  • 16
  • 67
  • 108
43
votes
3 answers

SQL MAX function in non-numeric columns

As far as I understand the MAX function, it shall return a maximum value from a given column. In case of numeric values, for example a salary column, it is clear for me - and this is the only application I find in tutorials. However, I have a…
Bartias
  • 431
  • 1
  • 4
  • 4
42
votes
2 answers

‘numeric_limits’ was not declared in this scope, no matching function for call to ‘max()’

I compiled this code at home on my mac w/ xcode and there was no provblem. I compile it at school with g++ on linux and I get these errors: numeric_limits’ is not a member of std expected primary-expression before ‘>’ token no matching function for…
Matt Munson
  • 2,903
  • 5
  • 33
  • 52
42
votes
4 answers

why is c++ std::max_element so slow?

I need to find the max element in the vector so I'm using std::max_element, but I've found that it's a very slow function, so I wrote my own version and manage to get x3 better performance, here is the code: #include #include…
MoonBun
  • 4,322
  • 3
  • 37
  • 69
41
votes
5 answers

Python: take max N elements from some list

Is there some function which would return me the N highest elements from some list? I.e. if max(l) returns the single highest element, sth. like max(l, count=10) would return me a list of the 10 highest numbers (or less if l is smaller). Or what…
Albert
  • 65,406
  • 61
  • 242
  • 386
41
votes
3 answers

Pandas max value index

I have a Pandas DataFrame with a mix of screen names, tweets, fav's etc. I want find the max value of 'favcount' (which i have already done) and also return the screen name of that 'tweet' df = pd.DataFrame() df['timestamp'] = timestamp df['sn'] =…
mGarsteck
  • 671
  • 2
  • 6
  • 16
41
votes
9 answers

Find highest value in multidimensional array

The Problem I have a multidimensional array similar to the one below. What I'm trying to achieve is a way to find and retrieve from the array the one with the highest "Total" value, now I know there's a function called max but that doesn't work with…
Karl
  • 5,435
  • 11
  • 44
  • 70
40
votes
10 answers

Why does Math.min() return positive infinity, while Math.max() returns negative infinity?

When I type in an array into the parameter of the javascript math minimum and maximum functions, it returns the correct value: console.log( Math.min( 5 ) ); // 5 console.log( Math.max( 2 ) ); // 2 var array = [3, 6, 1, 5, 0, -2, 3]; var minArray =…
auroranil
  • 2,621
  • 6
  • 24
  • 34
39
votes
7 answers

get min and max from a specific column scala spark dataframe

I would like to access to the min and max of a specific column from my dataframe but I don't have the header of the column, just its number, so I should I do using scala ? maybe something like this : val q = nextInt(ncol) //we pick a random value…
Laure D
  • 857
  • 2
  • 9
  • 16
39
votes
3 answers

Element-wise array maximum function in NumPy (more than two arrays)

I'm trying to return maximum values of multiple array in an element-wise comparison. For example: A = array([0, 1, 2]) B = array([1, 0, 3]) C = array([3, 0, 4]) I want the resulting array to be array([3,1,4]). I wanted to use numpy.maximum, but it…
Wang Yuan
  • 453
  • 1
  • 5
  • 8
38
votes
7 answers

max date record in LINQ

I have this table named sample with these values in MS Sql Server: ID Date Description 1 2012/01/02 5:12:43 Desc1 2 2012/01/02 5:12:48 Desc2 3 2012/01/03 5:12:41 Desc3 4 2012/01/03 5:12:43 Desc4 Now I want to write…
Salah Sanjabian
  • 745
  • 4
  • 10
  • 16
37
votes
13 answers

How to SELECT by MAX(date)?

This is the table structure: CREATE TABLE `reports` ( `report_id` int(11) NOT NULL auto_increment, `computer_id` int(11) NOT NULL default '0', `date_entered` datetime NOT NULL default '1970-01-01 00:00:00', `total_seconds` int(11) NOT NULL…
poetter747
  • 419
  • 1
  • 4
  • 6
37
votes
5 answers

std::max() and std::min() not constexpr

I just noticed that the new standard defines min(a,b) and max(a,b) without constexpr. Examples from 25.4.7, [alg.min.max]: template const T& min(const T& a, const T& b); template T min(initializer_list t); Isn't this a pity? I…
towi
  • 21,587
  • 28
  • 106
  • 187
37
votes
3 answers

Set maximum value (upper bound) in pandas DataFrame

I'm trying to set a maximum value of a pandas DataFrame column. For example: my_dict = {'a':[10,12,15,17,19,20]} df = pd.DataFrame(my_dict) df['a'].set_max(15) would yield: a 0 10 1 12 2 15 3 15 4 15 5 15 But it doesn't. There…
elPastor
  • 8,435
  • 11
  • 53
  • 81