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

Why is max and min of numpy array nan?

What could be the reason, why the max and min of my numpy array is nan? I checked my array with: for i in range(data[0]): if data[i] == numpy.nan: print("nan") And there is no nan in my data. Is my search wrong? If not: What could…
Jackie
  • 31
  • 3
2
votes
1 answer

MySQL SELECT value before MAX

How to select 1st, 2nd or 3rd value before MAX ? usually we do it with order by and limit SELECT * FROM table1 ORDER BY field1 DESC LIMIT 2,1 but with my current query I don't know how to make it... Sample table +----+------+------+-------+ | id |…
davispuh
  • 1,419
  • 3
  • 18
  • 30
2
votes
1 answer

Preventing Max function from using timestamp as part of criteria on a date column in PL/SQL

If I query: select max(date_created) date_created on a datefield in PL/SQL (Oracle 11g), and there are records that were created on the same date but at different times, Max() returns only the latest times on that date. What I would like to do is…
userDrew
  • 65
  • 1
  • 2
  • 7
2
votes
1 answer

Lucene and Elasticsearch going past the document limit

What happens when we try to ingest more documents into 'Lucene' instance past its max limit of 2,147,483,519? I read that as we approach closer to 2 billion documents we start seeing performance degradation. But does 'Lucene' just stop accepting new…
Shravan J Kumar
  • 158
  • 1
  • 1
  • 10
2
votes
1 answer

Play! - max constraint doesn't work

I'm having a problem with my application and @Max constraint annotation. My controller method is defined like this: public static void save(@Required @Max(255) String content) Later in my code I have error check: if (Validation.hasErrors()) { …
jjczopek
  • 3,319
  • 2
  • 32
  • 72
2
votes
1 answer

SQL insert into another table migration with group by having max()

I got the following problem. I got two tables and want to move one column data to another table. The tables are house and door. I want to move the column data 'street' to house. But there are multiple doors for the house (with same house_id). So I…
Ringo777
  • 97
  • 7
2
votes
4 answers

Elegantly calculate the minimum and maximum of a number array simultaneously

In Javascript, given a simple number[] array, is there an elegant way to calculate the minimum and maximum values simultaneously, or return undefined if the array is empty? Something like this: const a = [1, 2, 3]; const (min, max) =…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
2
votes
4 answers

max and group by question with LINQ

I want to group the below query by GetSetDomainName and select the row which has the maximum GetSetKalanGun.In other words, I am trying to get the row with the maximum KALANGUN among those which have the same DOMAINNAME. var kayitlar3 = ( from…
Bastardo
  • 4,144
  • 9
  • 41
  • 60
2
votes
3 answers

Pandas conditional creation of a dataframe column: based on multiple conditions max

I have a df: dog1 dog2 cat1 cat2 ant1 ant2 0 1 2 3 4 5 6 1 1 2 3 4 0 0 2 3 3 3 3 3 3 3 4 3 2 1 1 0 I want to add a new column based on the following…
saga
  • 736
  • 2
  • 8
  • 20
2
votes
1 answer

Kotlin: maxBy{} with optimum-value

Let's say I have the following code in Kotlin: val min = listOf("hello", "", "teeeeeest").minBy { it.length } What I understand from the implementation of minBy is that it tracks minValue in a variable and iterates through the whole collection and…
Moritz Groß
  • 1,352
  • 12
  • 30
2
votes
3 answers

numpy.maximum() on two 3-D arrays

I'm looking for the numpy-way to compare two 3-D arrays and return a new 3-D array containing the element-wise maxima based on a given index of the array. Here my value is in [y][x][7] for instance: output = numpy.zeros((16, 16, 8)) # we want to…
Roms
  • 23
  • 4
2
votes
1 answer

Dojo NumberTextBox : defining max constraint

I'm having troubles with the NumberTextBox max constraint. If I use standard markup declaration and create the numbertextbox with default setting, e.g. dojo.require("dijit.form.NumberTextBox");
belzebu
  • 810
  • 7
  • 25
2
votes
0 answers

Maximal contiguous subarray with at most K elements

Is there a way to find in O(N) complexity, a maximal contiguous subarray with at most K elements. Or there isn't such a possibility. for example: A = [4, -2, 6, 4, -2, 6] K = 3 because 6 and 4 in the middle are equal to 10, the function should…
Avraham
  • 61
  • 1
  • 4
2
votes
1 answer

Returning values from a column based on the last value of another column

I have a dataset like this: data <- data.frame(Time = c(1,4,6,9,11,13,16, 25, 32, 65), A = c(10, NA, 13, 2, 32, 19, 32, 34, 93, 12), B = c(1, 99, 32, 31, 12, 13, NA, 13, NA, NA), C = c(2, 32,…
Drew
  • 563
  • 2
  • 8
2
votes
1 answer

SQL Server varchar(max) is not working properly

In the following example, the txt column length is not correctly shown. CREATE TABLE dbo.test ( Id int IDENTITY, txt varchar(max) ); insert into test(txt) select REPLICATE('0',8000); insert into test(txt) select…
magick
  • 23
  • 1
  • 2