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
1375
votes
29 answers

Getting key with maximum value in dictionary?

I have a dictionary where keys are strings, and values are integers. stats = {'a': 1, 'b': 3000, 'c': 0} How do I get the key with the maximum value? In this case, it is 'b'. Is there a nicer approach than using an intermediate list with reversed…
ricafeal
  • 14,169
  • 4
  • 19
  • 10
1203
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
  • 12,605
  • 4
  • 21
  • 16
908
votes
22 answers

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

I have a table of player performance: CREATE TABLE TopTen ( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, home INT UNSIGNED NOT NULL, `datetime`DATETIME NOT NULL, player VARCHAR(6) NOT NULL, resource INT NOT NULL ); What query will return…
Kaptah
  • 9,785
  • 4
  • 22
  • 19
797
votes
21 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], then nargmax(array, n=3) would…
Alexis Métaireau
  • 10,767
  • 5
  • 24
  • 34
692
votes
25 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
  • 14,084
  • 7
  • 28
  • 23
683
votes
3 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
  • 27,513
  • 23
  • 77
  • 91
623
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
  • 30,624
  • 33
  • 94
  • 146
475
votes
15 answers

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

How do I find all rows in a pandas DataFrame which have the max value for count column, after grouping by ['Sp','Mt'] columns? Example 1: the following DataFrame: Sp Mt Value count 0 MM1 S1 a **3** 1 MM1 S1 n 2 2 MM1 S3 …
jojo12
  • 4,853
  • 3
  • 14
  • 7
437
votes
31 answers

How can I set max-length in an HTML5 "input type=number" element?

For element, maxlength is not working. How can I restrict the maxlength for that number element?
Prasad
  • 58,881
  • 64
  • 151
  • 199
407
votes
16 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
  • 112,946
  • 110
  • 377
  • 526
351
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
  • 11,940
  • 14
  • 50
  • 63
350
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
  • 33,129
  • 48
  • 101
  • 125
335
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
  • 4,081
  • 4
  • 21
  • 27
312
votes
33 answers

Write a program to find 100 largest numbers out of an array of 1 billion numbers

I recently attended an interview where I was asked "write a program to find 100 largest numbers out of an array of 1 billion numbers." I was only able to give a brute force solution which was to sort the array in O(nlogn) time complexity and take…
userx
  • 3,713
  • 5
  • 32
  • 38
269
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,853
  • 3
  • 17
  • 8
1
2 3
99 100