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

group by order by max()

I'm using MySQL. The result I want, is to display the row with the highest 'time' where 'res' = 'hans', and group the 'frm'. I am trying to fiddle around with GROUP BY, ORDER BY, MAX(time) - and I'm going no where. My table: 'messages' | frm | res …
CitronAutobot
  • 225
  • 2
  • 10
2
votes
2 answers

why both the sql query on rownum are giving different result

>SELECT instmax FROM (SELECT instmax, rownum r FROM ( SELECT * FROM pswlinstmax ORDER BY instmax DESC NULLS LAST ) ) WHERE r = 2; INSTMAX ------- 1049 >SELECT instmax FROM (SELECT instmax, …
Kumar
  • 855
  • 1
  • 12
  • 21
2
votes
1 answer

Getting the monthly maximum of a daily dataframe with the corresponding index value

I have dowloaded daily data from yahoo finance Open High Low Close Volume \ Date 2016-01-04 10485.809570 10485.910156 …
Markus W
  • 1,181
  • 5
  • 15
  • 30
2
votes
1 answer

Weighted Interval Scheduling problem & Dynamic program

My question is related to this other discussion. I'm trying to implement that algorithm using the dynamic program into a recursive call. Problem statement: Job j starts at sj, finishes at fj,and has weight or value vj. Two jobs compatible if they…
Francesco Laurita
  • 22,784
  • 7
  • 52
  • 63
2
votes
2 answers

Min and max of a column duplicated across all rows

I have an orders table with columns supplier_id and revenue: A table of total revenue per supplier would be as follows: SELECT supplier_id, SUM(revenue) as total_revenue FROM orders GROUP BY supplier_id I would like to normalize the…
Adler Santos
  • 225
  • 4
  • 17
2
votes
2 answers

How to find the maximum value of a numpy array between given indicies?

I have loaded some data from a csv using numpy.genfromtxt. This gives me an array(n,3) where n can be upto ~500000. I want to find the maximum value in any of the three columns between values of n=n1 and n=n2. In Matlab I'd be fine, but Python is a…
zotty
  • 697
  • 9
  • 24
2
votes
1 answer

min and max in legacy C++

I am trying to compile some older C++ code (probably from around 2001-2002) on a Debian GNU/Linux stable system. When compiling, I get the error: In file included from /usr/include/c++/4.7/vector:66:0, from ../FooMath/FooBar.h:23, …
2
votes
1 answer

Sequential number and maximum value using DENSE_RANK

I am using DENSE_RANK to assign a sequential number to different values of [y] within each group [x]. It adds a new column [rank]. I also want a new column showing the maximum rank [y] (highest sequence number) within each [x] e.g. [highest_rank].…
user2964644
  • 189
  • 9
2
votes
2 answers

Finding an item in a list with maximum value

I m a beginner in VB.Net. In the code I m working on, there is a class called Market with an attribute of Demand (integer). I have made a list of all instances of Market. At some point in the code, I need to find the Market instance in the list with…
Helia
  • 35
  • 1
  • 4
2
votes
2 answers

Python Dictionary min max

I have a question regarding this code: The number of elements in the dict vary at every iteration: d["one"] = 12 d["two"] = 13 d["three"] = 14 maxkey, maxvalue = max(d.iteritems(), key=lambda x:x[1]) minkey, minvalue = min(d.iteritems(), key=lambda…
Kenni
  • 317
  • 1
  • 2
  • 12
2
votes
4 answers

Select second MAX value

I want to select second highest value from tblTasks(JobID, ItemName, ContentTypeID) That's what I though of. I bet it can be done easier but I don't know how. SELECT Max(JobID) AS maxjobid, Max(ItemName) AS maxitemname, …
Kivayan
  • 65
  • 1
  • 6
2
votes
2 answers

Join on foreign key of table with the max primary key of that table

I have a workers table and an events table. Workers.ID = Events.WorkerID. I need to join these tables in a query but i need to do so on the MAX(ID) of the events table. I can't figure out how to do this. My base attempt: SELECT ID, Fname, SName FROM…
user2363025
  • 5,389
  • 15
  • 41
  • 86
2
votes
1 answer

Python: value > value returns true when it plainly isn't

I kept having errors raised while using a module, so I went to the line that was raising the exception and here it is: elif value > max(self._intervals): raise Exception("\nERROR: Value Is Greater Than Maximum Element In Points List") …
linus72982
  • 1,318
  • 14
  • 26
2
votes
2 answers

What is the max value of Int in different iOS versions?

I was building this app and it needs to convert a string to an Int. So I did this: Int(someString) However, sometimes someString is a very large number. Its value exceeds the max value of Int32. I know that the max value of Int can be different if…
Sweeper
  • 145,870
  • 17
  • 129
  • 225
2
votes
1 answer

Pythonic maxby function?

A maxby(list, f) takes a list of objects and a function f as arguments, such that f applied to any object in the list returns a number. Then maxby returns the object x in the list for which f(x) is maximum. What's the pythonic way to write something…
becko
  • 13,246
  • 24
  • 74
  • 144
1 2 3
99
100