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

How do you obtain the maximum possible date in Oracle?

Is there a function built into Oracle that will return the highest possible date that may be inserted into a date field?
Glenn Wark
  • 1,499
  • 4
  • 19
  • 23
27
votes
2 answers

Octave position of maximum value in column

I want to find the argmax of the values in a matrix by column, e.g.: 1 2 3 2 3 3 4 5 6 -> 3 7 8 I feel like I should just be able to map an argmax/posmax function over the columns, but I don't see a particularly intuitive way to do this in…
Philip Massey
  • 1,401
  • 3
  • 14
  • 24
27
votes
4 answers

How consider NULL as the MAX date instead of ignoring it in MySQL?

Lets say that I have a table named test like this: ID DATE 1 '2013-01-26' 1 NULL 1 '2013-03-03' 2 '2013-02-23' 2 '2013-04-12' 2 '2013-05-02' And I would like to get from this table : ID DATE …
user2741700
  • 881
  • 3
  • 11
  • 21
26
votes
1 answer

find max value of a list with numpy nan

import numpy as np print max([np.nan, 1, 2, 3, 4]) print max([1, 2, 3, 4, np.nan]) print max([1, 2, 3, np.nan, 4]) the first will print nan as list's max value the second will print 4 as list's max value the third will print 4 as list's max…
hihell
  • 936
  • 1
  • 10
  • 20
26
votes
8 answers

What is the easiest way to get a key with the highest value from a hash in Perl?

What is the easiest way to get a key with the highest value from a hash in Perl?
syker
  • 10,912
  • 16
  • 56
  • 68
26
votes
4 answers

Java Math.min/max performance

EDIT: maaartinus gave the answer I was looking for and tmyklebu's data on the problem helped a lot, so thanks both! :) I've read a bit about how HotSpot has some "intrinsics" that injects in the code, specially for Java standard Math libs (from…
TheStack
  • 553
  • 4
  • 12
25
votes
9 answers

How to refresh a jQuery UI Slider after setting min or max values?

I have a ui.slider and change its min and max values on runtime. But these changes only get reflected in the view, when I set the values afterwards too (which causes it to fire events that are not needed). In my opinion, it should refresh the view…
Manuel Leuenberger
  • 2,327
  • 3
  • 21
  • 27
25
votes
4 answers

PyTorch torch.max over multiple dimensions

Have tensor like :x.shape = [3, 2, 2]. import torch x = torch.tensor([ [[-0.3000, -0.2926],[-0.2705, -0.2632]], [[-0.1821, -0.1747],[-0.1526, -0.1453]], [[-0.0642, -0.0568],[-0.0347, -0.0274]] ]) I need to take .max() over the 2nd and…
iGero
  • 383
  • 1
  • 3
  • 6
25
votes
9 answers

Get max value from a list with lists?

So I have a list that contains several list which all have three strings first, then one float number, like: resultlist = [["1", "1", "a", 8.3931], ["1", "2", "b", 6.3231], ["2", "1", "c", 9.1931]] How do I make a function that returns the maximum…
FeatherMarauder
  • 265
  • 1
  • 3
  • 8
25
votes
9 answers

Countdown available spaces in a textarea with jquery or other?

I have a few areas on my site where I need to limit text input to X amount of characters and it's nice to show a number of spaces left while a user types in, like twitter does. I found this jquery plugin; jquery max-length plugin It does just what I…
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
24
votes
5 answers

Aggregate Relational Algebra (Maximum)

I am currently working on a homework assignment that requires a selection to occur that pulls out an element containing a specific attribute of maximum value compared to all other records. I've read a number of sources online that reference an…
XBigTK13X
  • 2,655
  • 8
  • 30
  • 39
24
votes
5 answers

Find min, max, and average of a list

I am having hard time to figure out how to find min from a list for example somelist = [1,12,2,53,23,6,17] how can I find min and max of this list with defining (def) a function I do not want to use built-in function min
mtkilic
  • 1,213
  • 1
  • 12
  • 28
23
votes
5 answers

Find maximum value and index in a python list?

I have a python list that is like this, [[12587961, 0.7777777777777778], [12587970, 0.5172413793103449], [12587979, 0.3968253968253968], [12587982, 0.88], [12587984, 0.8484848484848485], [12587992, 0.7777777777777778], [12587995,…
rksh
  • 3,920
  • 10
  • 49
  • 68
23
votes
6 answers

How can I find the index of the maximum value in a List in Scala?

For a Scala List[Int] I can call the method max to find the maximum element value. How can I find the index of the maximum element? This is what I am doing now: val max = list.max val index = list.indexOf(max)
Phil
  • 46,436
  • 33
  • 110
  • 175
23
votes
18 answers

Mathematically Find Max Value without Conditional Comparison

----------Updated ------------ codymanix and moonshadow have been a big help thus far. I was able to solve my problem using the equations and instead of using right shift I divided by 29. Because with 32bits signed 2^31 = overflows to 29. Which…
Almost Famous
  • 569
  • 1
  • 6
  • 19