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
23
votes
1 answer

How to store a string var greater than varchar(max)?

I'm trying to do this: DECLARE @myVar VARCHAR(MAX) Loop with cursor select @myVar = @myVar + bla bla bla end loop When the loop ends, @myVar is incomplete, containing only 8000 characters. I have tryed to use text, but is not allowed to local…
Eduardo
  • 693
  • 3
  • 9
  • 21
22
votes
7 answers

Avoiding concurrency problems with MAX+1 integer in SQL Server 2008... making own IDENTITY value

I need to increment an integer in a SQL Server 2008 column. Sounds like I should use an IDENTITY column, but I need to increment separate counters for each of my customers. Think of an e-commerce site where each customer gets their own…
stackonfire
  • 1,525
  • 4
  • 17
  • 23
22
votes
11 answers

How to use "Partition By" or "Max"?

I've the following table (my_data): year | X | Y -----+-----+----- 2010 | A | 10 2011 | A | 20 2011 | B | 99 2009 | C | 30 2010 | C | 40 what is the best / smallest SQL statement to retrieve only the data related to the highest…
Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
22
votes
3 answers

How to find maximum value in whole 2D array with indices

I want to find the maximum value in a 2D array and the indices of the maximum value in Python using NumPy. I used np.amax(array) for searching for the maximum value, but I don't know how to get its indices. I could find it by using ´for` loops, but…
Max
  • 825
  • 2
  • 7
  • 7
22
votes
7 answers

How would you define a simple "min" method in obj-c

I want to define a min and max methods in a Utils class. @interface Utils int min(int a, int b); int max(int a, int b); @end But I don't want to have named parameters. It would be a too heavy notation. I wanted to use the C-style definition. But…
Fred Polack
  • 223
  • 1
  • 2
  • 4
22
votes
7 answers

how to select max of mixed string/int column?

Lets say that I have a table which contains a column for invoice number, the data type is VARCHAR with mixed string/int values like: invoice_number ************** HKL1 HKL2 HKL3 ..... HKL12 HKL13 HKL14 HKL15 I tried…
CairoCoder
  • 3,091
  • 11
  • 46
  • 68
22
votes
2 answers

How do I write a MAX query with a where clause in JPA 2.0?

I'm using JPA 2.0. Hibernate 4.1.0.Final, and Java 6. How do I write a JPA query from the following psuedo-SQL? select max(e.dateProcessed) from Event e where e.org = myOrg And my domain object looks like the following: @GenericGenerator(name =…
Dave
  • 15,639
  • 133
  • 442
  • 830
21
votes
2 answers

Maximum file size of JPEG image with known dimensions

I'm going to let users upload images of 300x300 compressed with JPEG. Is there a way to determine what the maximum file size of such an image would be? I can imagine this can be tried by compressing random noise at 100 quality, but is there a…
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
21
votes
3 answers

How to extract the n-th maximum/minimum value in a column of a DataFrame in pandas?

I would like to obtain the n-th minimum or the n-th maximum value from numerical columns in the DataFrame in pandas. Example: df = pd.DataFrame({'a': [3.0, 2.0, 4.0, 1.0],'b': [1.0, 4.0 , 2.0, 3.0]}) a b 0 3.0 1.0 1 2.0 4.0 2 4.0 …
Krzysztof Słowiński
  • 6,239
  • 8
  • 44
  • 62
21
votes
5 answers

How to find the largest N elements in a list in R?

I have a list of floats in R. For a given integer, N, I want to find the indices of the largest N values in my list. So for example, if N is 2, I want to find the indices of the two largest values in my list. How do I do this? I cannot reorder my…
user2560984
  • 361
  • 2
  • 6
  • 12
21
votes
7 answers

indices of the k largest elements in an unsorted length n array

I need to find the indices of the k largest elements of an unsorted, length n, array/vector in C++, with k < n. I have seen how to use nth_element() to find the k-th statistic, but I'm not sure if using this is the right choice for my problem as it…
Luke Peterson
  • 931
  • 1
  • 9
  • 25
21
votes
9 answers

Python Finding Index of Maximum in List

def main(): a = [2,1,5,234,3,44,7,6,4,5,9,11,12,14,13] max = 0 for number in a: if number > max: max = number print max if __name__ == '__main__': main() I am able to get the maximum value in the array…
Shankar Kumar
  • 2,197
  • 6
  • 25
  • 32
20
votes
6 answers

Using Python's max to return two equally large values

I'm using Python's max function to find the largest integer in a dictionary called count, and the corresponding key (not quite sure if I'm saying it properly; my code probably explains itself better than I'm explaining it). The dictionary count is…
kidosu
  • 383
  • 2
  • 4
  • 9
20
votes
1 answer

Is there a numpy "max minus min" function?

Is there a numpy function that gives for a given numpy array its maximum - minimum value, i.e. numpy.max(a) - numpy.min(a) ? e.g. numpy.xxx([4,3,2, 6] = 4 since max = 6, min = 2, 6 - 4 = 2) Reason: performance increase since max and min would…
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
20
votes
9 answers

Mysql in PHP - how to update only one row in table but with greatest id number

I am trying to update fields in my DB, but got stuck with such a simple problem: I want to update just one row in the table with the biggest id number. I would do something like that: UPDATE table SET name='test_name' WHERE id =…
Kalreg
  • 931
  • 3
  • 12
  • 31