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
72
votes
10 answers

How to find the highest value of a column in a data frame in R?

I have the following data frame which I called ozone: Ozone Solar.R Wind Temp Month Day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12 149 12.6 74 5 3 4 18 313 11.5 62 5 4 5 NA …
Al V
  • 1,227
  • 2
  • 11
  • 15
68
votes
7 answers

return max value from pandas dataframe as a whole, not based on column or rows

I am trying to get the max value from a panda dataframe as a whole. I am not interested in what row or column it came from. I am just interested in a single max value within the DataFrame. Here is my DataFrame: df = pd.DataFrame({'group1':…
Boosted_d16
  • 13,340
  • 35
  • 98
  • 158
67
votes
11 answers

Set UITextField Maximum Length

Is there any way to set the maximum length on a UITextField? Something like the MAXLENGTH attribute in HTML input fields.
EldenChris
  • 1,059
  • 1
  • 10
  • 16
66
votes
3 answers

Postgresql turn null into zero

Possible Duplicate: SELECT max(x) is returning null; how can I make it return 0? When I execute select max(column) from mytable; and my table has no rows, it returns null. How can I amend this select statement so it will return zero?
maverick
  • 2,902
  • 2
  • 19
  • 15
66
votes
5 answers

Does sleep time count for execution time limit?

I have two questions concerning the sleep() function in PHP: Does the sleep time affect the maximum execution time limit of my PHP scripts? Sometimes, PHP shows the message "maximum execution time of 30 seconds exceeded". Will this message appear…
caw
  • 30,999
  • 61
  • 181
  • 291
66
votes
11 answers

Getting the submatrix with maximum sum?

Input: A 2-dimensional array NxN - Matrix - with positive and negative elements.Output: A submatrix of any size such that its summation is the maximum among all possible submatrices. Requirement: Algorithm complexity to be of O(N^3) History: With…
guirgis
  • 1,133
  • 2
  • 14
  • 17
65
votes
7 answers

In Ruby, what is the cleanest way of obtaining the index of the largest value in an array?

If a is the array, I want a.index(a.max), but something more Ruby-like. It should be obvious, but I'm having trouble finding the answer at so and elsewhere. Obviously, I am new to Ruby.
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
63
votes
5 answers

How to limit a number to be within a specified range? (Python)

I want to limit a number to be within a certain range. Currently, I am doing the following: minN = 1 maxN = 10 n = something() #some return value from a function n = max(minN, n) n = min(maxN, n) This keeps it within minN and maxN, but it doesn't…
Mantis Toboggan
  • 7,295
  • 3
  • 19
  • 11
62
votes
9 answers

Finding the max value in a map

I've been doing a basic program to find the max, min, median, variance, mode etc. of a vector. Everything went fine until I got to the mode. The way I see it, I should be able to loop through the vector, and for each number that occurs I increment a…
Sh0gun
  • 871
  • 4
  • 10
  • 16
62
votes
8 answers

Getting the max value of attributes from a list of objects

I have this list of objects which have a x and a y parameter (and some other stuff). path.nodes = ( , , ,
PDXIII
  • 1,086
  • 2
  • 8
  • 17
60
votes
6 answers

What is the maximum number of characters for a host-name in Unix?

I am wondering what is the maximum number of characters for a host-name in a Unix system. In addition is there any defined variable that can be used in Unix programming to call that number? (i.e. number of characters allowed for a host-name). I am…
CompilingCyborg
  • 4,760
  • 13
  • 44
  • 61
60
votes
6 answers

warning C4003 and errors C2589 and C2059 on: x = std::numeric_limits::max();

This line works correctly in a small test program, but in the program for which I want it, I get the following compiler complaints: #include x = std::numeric_limits::max(); c:\...\x.cpp(192) : warning C4003: not enough actual…
Harvey
  • 2,062
  • 2
  • 21
  • 38
60
votes
1 answer

How to find item with max value using linq?

Have a look at the below table: Item Value A 10 b 50 c 90 I want to find the item with maximum value. I can get that by using group by or orderding, but somehow I have a feeling there should be…
user1784622
  • 639
  • 1
  • 5
  • 9
57
votes
3 answers

How can I take pairwise parallel maximum or minimum between two vectors?

Suppose I have two vectors in R, defined as follows. a = c(3,3,5) b = c(2,4,6) Is there a function that will give me the pairwise maximum between the elements of a and the elements of b, which can be run inside a formula? I tried to do, max(a, b)…
merlin2011
  • 71,677
  • 44
  • 195
  • 329
56
votes
10 answers

Min and max value of input in angular4 application

I have an angular4 application with a form. In this one I have an input to enter a percentage. So, I want to block the input with value between 0 and 100. I tried to add min="0" and max="100" but I can yet enter an number higher than 100 or smaller…
Adrien
  • 2,866
  • 4
  • 23
  • 46