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

How to make numpy.argmax return all occurrences of the maximum?

I'm trying to find a function that returns all occurrences of the maximum in a given list. numpy.argmax however only returns the first occurrence that it finds. For instance: from numpy import argmax list = [7, 6, 5, 7, 6, 7, 6, 6, 6, 4, 5,…
Marieke_W
  • 941
  • 1
  • 7
  • 5
89
votes
8 answers

How many socket connections possible?

Has anyone an idea how many tcp-socket connections are possible on a modern standard Linux server? (There is in general less traffic on each connection, but all the connections have to be up all the time.)
TheHippo
  • 61,720
  • 15
  • 75
  • 100
89
votes
2 answers

Maximum size of a PHP session

What is the maximum size that can be stored in a PHP session?
Roch
  • 21,741
  • 29
  • 77
  • 120
87
votes
12 answers

What is the maximum number of edges in a directed graph with n nodes?

What is the maximum number of edges in a directed graph with n nodes? Is there any upper bound?
Loolooii
  • 8,588
  • 14
  • 66
  • 90
87
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,572
  • 4
  • 24
  • 23
85
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
  • 5,406
  • 7
  • 31
  • 35
81
votes
19 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,532
  • 12
  • 45
  • 49
81
votes
5 answers

Why is math.max() returning NaN on an array of integers?

I am trying to get the highest number from a simple array: data = [4, 2, 6, 1, 3, 7, 5, 3]; alert(Math.max(data)); I have read that if even one of the values in the array can't be converted to number, it will return NaN, but in my case, I have…
Matt Welander
  • 8,234
  • 24
  • 88
  • 138
78
votes
4 answers

MySQL: get MAX or GREATEST of several columns, but with NULL fields

I'm trying to select the max date in three different fields in each record (MySQL) So, in each row, I have date1, date2 and date3: date1 is always filled, date2 and date3 can be NULL or empty The GREATEST statement is simple and concise but has no…
Ivan
  • 2,463
  • 6
  • 39
  • 51
77
votes
5 answers

Which maximum does Python pick in the case of a tie?

When using the max() function in Python to find the maximum value in a list (or tuple, dict etc.) and there is a tie for maximum value, which one does Python pick? Is it random? This is relevant if, for instance, one has a list of tuples and one…
Double AA
  • 5,759
  • 16
  • 44
  • 56
76
votes
5 answers

Maximum length of a domain name without the http://www. & .com parts

What is the maximum length of the 'name' part in a domain? I'm referring to the google in http://www.google.com. How long can the google part be without what's before and after it?
Norman
  • 6,159
  • 23
  • 88
  • 141
76
votes
2 answers

PostgreSQL MAX and GROUP BY

I have a table with id, year and count. I want to get the MAX(count) for each id and keep the year when it happens, so I make this query: SELECT id, year, MAX(count) FROM table GROUP BY id; Unfortunately, it gives me an error: ERROR: column…
Project Dumbo Dev
  • 763
  • 1
  • 5
  • 5
75
votes
14 answers

Find the item with maximum occurrences in a list

In Python, I have a list: L = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 4, 5456, 56, 6, 7, 67] I want to identify the item that occurred the highest number of times. I am able to solve it but I need the fastest way to do so. I know there is a nice…
zubinmehta
  • 4,368
  • 7
  • 33
  • 51
75
votes
3 answers

Using std::max_element on a vector

I'm trying to use std::min_element and std::max_element to return the minimum and maximum elements in a vector of doubles. My compiler doesn't like how I'm currently trying to use them, and I don't understand the error message. I could of course…
synaptik
  • 8,971
  • 16
  • 71
  • 98
74
votes
5 answers

5 maximum values in a python dictionary

I have a dictionary like this: A = {'a':10, 'b':843, 'c': 39,.....} I want to get the 5 maximum values of this dict and store a new dict with this. To get the maximum value I did: max(A.iteritems(), key=operator.itemgetter(1))[0:] Perhaps it is an…
Alejandro
  • 4,945
  • 6
  • 32
  • 30