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
2
votes
2 answers

Finding biggest and smallest difference between 1st and 2nd element of 2d array elements

I made a function which takes a list of 2d elements(containing lists with 2 elements) as an argument and returns the element(or elements) whose elements' difference is the biggest and the one(or more) whose elements' difference is the smallest. For…
Georgy90
  • 155
  • 2
  • 12
2
votes
1 answer

Max Value Per Row from a set of 100 Columns in SQL

While I have seen several answers to this question all of them tend to rely on on the greatest function which keeps coming back with a "Msg 195, Level 15, State 10, Line 1 'GREATEST' is not a recognized built-in function name." I would like to know…
Artina
  • 21
  • 2
2
votes
0 answers

Scala: Find the maximum value across each row of a dataframe

For each row of a DataFrame, I would like to extract the maximum value and put it in a new column. The example code below gives me a DataFrame ('dfmax') of each maximum value: val donuts = Seq((2.0, 1.50, 3.5), (4.2, 22.3, 10.8), (33.6, 2.50,…
Christian
  • 991
  • 2
  • 13
  • 25
2
votes
1 answer

Matlab code to identify the maximum peak (coordinate) of a polynomial curve

I have this set of x and y data that would fit into a polynomial curve. Is there any way using Matlab (or Excel) to graph this data, trace a polynomial fit to it, and identify the (x,y) coordinate for the maximum peak of the curve; not the maximum…
2
votes
4 answers

Retrieving datalength along with columns and tables in SQL Server query

Because I want to convert the columns to not be varchar(MAX) I want to see the maximum datalength for each column to decide what the new size should be. I have this query for finding all my (n)varchar(MAX) columns. SELECT [TABLE_NAME],…
Daniel
  • 10,641
  • 12
  • 47
  • 85
2
votes
2 answers

Creating a CGRect that encompasses a UIBezierPath and NSArray x Array in the process

All I need to do is to create a CGRect that encompasses a UIBezierPath and I have an array of CGPoint. To create this CGRect all I need to do is to enumerate the array of CGPoint and find the minimum and maximum X and Y coordinates. Then I…
Duck
  • 34,902
  • 47
  • 248
  • 470
2
votes
2 answers

How to set Max Length on ag-grid cells

Is there any way to impose a maxLength text allowed in an ag-grid cell, similar to the one on a normal input element? No relative documentation was found. Also, no particular situations & more details are needed, in my…
Vlad Danila
  • 1,222
  • 3
  • 18
  • 38
2
votes
4 answers

SQL Max Value for a Specified Limit

I'm trying to return a list of years when certain conditions are met but I am having trouble with the MAX function and having it work with the rest of my logic. For the following two tables: coach coach | team | wins |…
Wintress
  • 67
  • 2
  • 9
2
votes
2 answers

SQL How to combine MAX and COUNT using inner query properly

I'm having a problem with selecting rows only with maximum values from column ProblemsAmount, which represents COUNT(*) from inner query. It looks like: PersonID | PersonName | ProblemID | ProblemsAmount 1 | Johny | 1 …
2
votes
2 answers

How to select rows with max values of 2 columns

I have a table mysql like this : CREATE TABLE prelevement ( line int, facture varchar(30), date_op varchar(30), code_op varchar(30) ); insert into prelevement (line,facture,date_op,code_op) values…
Sophia
  • 21
  • 1
2
votes
1 answer

MySQL - select max value with of sum over year

I have table like: v | day | month | year 1 1 1 1950 2 2 1 1950 3 3 1 1950 1 1 2 1950 1 1 2 1951 2 1 2 1952 I have used this query to select…
Martin Perry
  • 9,232
  • 8
  • 46
  • 114
2
votes
2 answers

How do I maximize a vector

I'd like to maximize numbers in a vector and get the results as a new vector Like this: w <- 0:10 maximizer <- function(w){ max(10, w + 5) } I'm expecting getting a vector (10,10,10,10,10,10,11,12,13,14,15), but all I'm getting is 15. I know…
Mondainai
  • 35
  • 2
2
votes
1 answer

Find min max over all columns without listing down each column name in SQL

I have a SQL table (actually a BigQuery table) that has a huge number of columns (over a thousand). I want to quickly find the min and max value of each column. Is there a way to do that? It is impossible for me to list all the columns. Looking for…
Nik
  • 5,515
  • 14
  • 49
  • 75
2
votes
1 answer

SQL Max (Quantity*ProductPrice)

I am trying to answer the question of which product ID and ProductName brought in more revenue than the average revenue earned from a product in the store. I have the following code: Select ProductName, SupplierName, MAX(Quantity*ProductPrice) as…
user10643490
2
votes
3 answers

Finding highest value of 3 keys in array with more keys

I have an array that looks like this: Array ( [id] => 12 [team_home_id] => 50 [team_away_id] => 63 [score_team_home] => 1 [score_team_away] => 1 [league_id] => 3 [home_win_pred] => 50 …
Awesom-o
  • 534
  • 6
  • 20
1 2 3
99
100