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();
7849 questions
1072
votes
8 answers

What is the maximum length of a valid email address?

What is the maximum length of a valid email address? Is it defined by any standard?
volatilevoid
  • 11,255
  • 4
  • 19
  • 16
1042
votes
26 answers

Getting key with maximum value in dictionary?

I have a dictionary: keys are strings, values are integers. Example: stats = {'a':1000, 'b':3000, 'c': 100} I'd like to get 'b' as an answer, since it's the key with a higher value. I did the following, using an intermediate list with reversed…
ricafeal
  • 10,839
  • 4
  • 17
  • 10
826
votes
20 answers

How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?

My table is: id home datetime player resource ---|-----|------------|--------|--------- 1 | 10 | 04/03/2009 | john | 399 2 | 11 | 04/03/2009 | juliet | 244 5 | 12 | 04/03/2009 | borat | 555 3 | 10 | 03/03/2009 | john | 300 4 …
Kaptah
  • 8,903
  • 4
  • 18
  • 17
602
votes
19 answers

How do I get indices of N maximum values in a NumPy array?

NumPy proposes a way to get the index of the maximum value of an array via np.argmax. I would like a similar thing, but returning the indexes of the N maximum values. For instance, if I have an array, [1, 3, 2, 4, 5], function(array, n=3) would…
Alexis Métaireau
  • 8,372
  • 5
  • 21
  • 34
592
votes
4 answers

What is the maximum possible length of a query string?

Is it browser dependent? Also, do different web stacks have different limits on how much data they can get from the request?
Brian Sullivan
  • 25,394
  • 22
  • 73
  • 88
561
votes
24 answers

Getting the index of the returned max or min item using max()/min() on a list

I'm using Python's max and min functions on lists for a minimax algorithm, and I need the index of the value returned by max() or min(). In other words, I need to know which move produced the max (at a first player's turn) or min (second player)…
Kevin Griffin
  • 11,989
  • 7
  • 26
  • 22
542
votes
31 answers

Is there a Max function in SQL Server that takes two values like Math.Max in .NET?

I want to write a query like this: SELECT o.OrderId, MAX(o.NegotiatedPrice, o.SuggestedPrice) FROM Order o But this isn't how the MAX function works, right? It is an aggregate function so it expects a single parameter and then returns the MAX of…
skb
  • 28,056
  • 29
  • 91
  • 140
397
votes
28 answers

How can I limit possible inputs in a HTML5 "number" element?

For element, maxlength is not working. How can I restrict the maxlength for that number element?
Prasad
  • 56,343
  • 61
  • 142
  • 199
343
votes
15 answers

MIN and MAX in C

Where are MIN and MAX defined in C, if at all? What is the best way to implement these, as generically and type safely as possible? (Compiler extensions/builtins for mainstream compilers preferred.)
Matt Joiner
  • 100,604
  • 94
  • 332
  • 495
313
votes
9 answers

How to perform .Max() on a property of all objects in a collection and return the object with maximum value

I have a list of objects that have two int properties. The list is the output of another linq query. The object: public class DimensionPair { public int Height { get; set; } public int Width { get; set; } } I want to find and return the…
theringostarrs
  • 10,520
  • 14
  • 47
  • 62
312
votes
11 answers

max value of integer

In C, the integer (for 32 bit machine) is 32 bits, and it ranges from -32,768 to +32,767. In Java, the integer(long) is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647. I do not understand how the range is different in Java, even…
stackuser
  • 3,841
  • 4
  • 17
  • 25
311
votes
3 answers

How to get the max of two values in MySQL?

I tried but failed: mysql> select max(1,0); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0)' at line 1
Mask
  • 29,767
  • 47
  • 97
  • 123
305
votes
13 answers

Get the row(s) which have the max value in groups using groupby

How do I find all rows in a pandas data frame which have the max value for count column, after grouping by ['Sp','Mt'] columns? Example 1: the following dataFrame, which I group by ['Sp','Mt']: Sp Mt Value count 0 MM1 S1 a **3** 1 …
jojo12
  • 3,153
  • 3
  • 12
  • 6
246
votes
8 answers

How to scale down a range of numbers with a known min and max value

So I am trying to figure out how to take a range of numbers and scale the values down to fit a range. The reason for wanting to do this is that I am trying to draw ellipses in a java swing jpanel. I want the height and width of each ellipse to be…
user650271
  • 2,615
  • 3
  • 15
  • 8
213
votes
31 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
  • 35,833
  • 64
  • 143
  • 195
1
2 3
99 100