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
145
votes
8 answers

How to do SELECT MAX in Django?

I have a list of objects how can I run a query to give the max value of a field: I'm using this code: def get_best_argument(self): try: arg = self.argument_set.order_by('-rating')[0].details except IndexError: return 'no…
Johnd
  • 6,441
  • 9
  • 29
  • 22
145
votes
5 answers

How to find the min/max value of a common key in a list of dicts?

I have a list of dictionaries like so: [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}] I want to find the min() and max() prices. Now, I can sort this easily enough using a key with a lambda expression (as found in…
Hank Fay
  • 1,704
  • 2
  • 11
  • 11
145
votes
20 answers

Max length UITextField

When I've tried How to you set the maximum number of characters that can be entered into a UITextField using swift?, I saw that if I use all 10 characters, I can't erase the character too. The only thing I can do is to cancel the operation (delete…
giorgionocera
  • 6,428
  • 6
  • 20
  • 17
142
votes
14 answers

Is there a method to find the max of 3 numbers in C#?

The method should work like Math.Max(), but take 3 or more int parameters.
JamesRedcoat
  • 2,083
  • 2
  • 15
  • 13
126
votes
10 answers

mongodb how to get max value from collections

I have a mongodb collection like: db.kids.find() //results [ {name:'tom', age:10}, {name:'alice', age:12}, .... ] I need a query to get MAX 'age' from this collection like in SQL: SELECT MAX(age) FROM kids WHERE 1
Hossain Khademian
  • 3,291
  • 3
  • 12
  • 10
126
votes
9 answers

Check if all values in list are greater than a certain number

my_list1 = [30,34,56] my_list2 = [29,500,43] How to I check if all values in list are >= 30? my_list1 should work and my_list2 should not. The only thing I could think of doing was: boolean = 0 def func(ls): for k in ls: if k >= 30: …
O.rka
  • 29,847
  • 68
  • 194
  • 309
122
votes
10 answers

How to get max value of a column using Entity Framework?

To get maximum value of a column that contains integer, I can use the following T-SQL comand SELECT MAX(expression ) FROM tables WHERE predicates; Is it possible to obtain the same result with Entity Framework. Let's say I have the following…
Richard77
  • 20,343
  • 46
  • 150
  • 252
108
votes
5 answers

SQL Query to get column values that correspond with MAX value of another column?

Ok, this is my query: SELECT video_category, video_url, video_date, video_title, short_description, MAX(video_id) FROM videos GROUP BY video_category When it pulls the data, I get the correct row for the video_id, but it pulls the…
Devin
  • 2,146
  • 5
  • 24
  • 36
106
votes
7 answers

Find the greatest (largest, maximum) number in a list of numbers

How can I easily find the greatest number in a given list of numbers? See also How do I find the maximum (larger, greater) of 2 numbers? - in that special case, the two values can also be compared directly.
Chris Foster
  • 2,639
  • 2
  • 23
  • 30
105
votes
7 answers

A safe max() function for empty lists

Evaluating, max_val = max(a) will cause the error, ValueError: max() arg is an empty sequence Is there a better way of safeguarding against this error other than a try, except catch? a = [] try: max_val = max(a) except ValueError: max_val…
Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
104
votes
2 answers

Why do I receive a DMARC report everyday?

I've setup DMARC policy on my domain. But every day I recieve an XML report from Google. I don't understand what the problem is? The report is:
IvanS
  • 1,204
  • 2
  • 8
  • 8
98
votes
10 answers

Group by minimum value in one field while selecting distinct rows

Here's what I'm trying to do. Let's say I have this table t: key_id | id | record_date | other_cols 1 | 18 | 2011-04-03 | x 2 | 18 | 2012-05-19 | y 3 | 18 | 2012-08-09 | z 4 | 19 | 2009-06-01 | a 5 | 19 | 2011-04-03 |…
user2765924
  • 983
  • 1
  • 6
  • 4
94
votes
1 answer

Python: Maximum recursion depth exceeded

I have the following recursion code, at each node I call sql query to get the nodes belong to the parent node. here is the error: Exception RuntimeError: 'maximum recursion depth exceeded' in
add-semi-colons
  • 18,094
  • 55
  • 145
  • 232
93
votes
3 answers

Why is max slower than sort?

I've found that max is slower than the sort function in Python 2 and 3. Python 2 $ python -m timeit -s 'import random;a=range(10000);random.shuffle(a)' 'a.sort();a[-1]' 1000 loops, best of 3: 239 usec per loop $ python -m timeit -s 'import…
WeizhongTu
  • 6,124
  • 4
  • 37
  • 51
91
votes
10 answers

Good way to get the key of the highest value of a Dictionary in C#

I'm trying to get the key of the maximum value in the Dictionary results. This is what I have so far: double max = results.Max(kvp => kvp.Value); return results.Where(kvp => kvp.Value == max).Select(kvp => kvp.Key).First(); However,…
Arda Xi
  • 2,616
  • 3
  • 19
  • 24