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
104
votes
5 answers

In List of Dicts, find min() value of a common Dict field

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,294
  • 2
  • 9
  • 11
103
votes
5 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
  • 5,433
  • 9
  • 26
  • 22
100
votes
9 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
  • 2,511
  • 3
  • 9
  • 10
98
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
  • 17,505
  • 36
  • 124
  • 222
95
votes
7 answers

Find the greatest number in a list of numbers

Is there any easy way or function to determine the greatest number in a python list? I could just code it, as I only have three numbers, however it would make the code a lot less redundant if I could tell the greatest with a built in function or…
Chris Foster
  • 2,441
  • 1
  • 19
  • 28
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
  • 5,086
  • 2
  • 31
  • 47
93
votes
8 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
  • 24,289
  • 52
  • 152
  • 253
88
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
  • 14,928
  • 43
  • 126
  • 211
81
votes
2 answers

Maximum size of a PHP session

What is the maximum size that can be stored in a PHP session?
Roch
  • 20,993
  • 27
  • 74
  • 118
80
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,477
  • 3
  • 17
  • 23
79
votes
8 answers

How many socket connections possible?

Has anyone an idea how many tcp-socket connections are possible on a modern standard root server? (There is in general less traffic on each connection, but all the connections have to be up all the time.) EDIT: We will use a Linux Server.
TheHippo
  • 54,987
  • 13
  • 72
  • 98
79
votes
20 answers

Explain this snippet which finds the maximum of two integers without using if-else or any other comparison operator?

Find the maximum of two numbers. You should not use if-else or any other comparison operator. I found this question on online bulletin board, so i thought i should ask in StackOverflow EXAMPLE Input: 5, 10 Output: 10 I found this solution, can…
SuperMan
  • 3,396
  • 10
  • 43
  • 49
79
votes
3 answers

Python Pandas add column for row-wise max value of selected columns

data = {'name' : ['bill', 'joe', 'steve'], 'test1' : [85, 75, 85], 'test2' : [35, 45, 83], 'test3' : [51, 61, 45]} frame = pd.DataFrame(data) I would like to add a new column that shows the max value for each row. desired output: name…
user2333196
  • 4,246
  • 6
  • 26
  • 31
79
votes
14 answers

Use of min and max functions in C++

From C++, are std::min and std::max preferable over fmin and fmax? For comparing two integers, do they provide basically the same functionality? Do you tend to use one of these sets of functions or do you prefer to write your own (perhaps to…
bporter
  • 3,302
  • 4
  • 21
  • 23
76
votes
4 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
  • 1,796
  • 4
  • 21
  • 35