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

min for each row in a data frame

I'm try to calculate minimum across multiple columns (row-wise min) in a data frame, but the min function automatically returns the minimum across the whole of each column rather than for each row separately. I'm sure I'm missing something really…
Rob
  • 599
  • 2
  • 6
  • 9
32
votes
4 answers

rowwise maximum for R

I have a dataframe as below. I want to get a column of maximums for each row. But that column should ignore value 9 if it is present in that row. How can i achive that efficiently? df <- data.frame(age=c(5,6,9), marks=c(1,2,7),…
user2543622
  • 5,760
  • 25
  • 91
  • 159
32
votes
3 answers

Select column value where other column is max of group

I am trying to select two columns into a table (ID and state). The table should show the state with the maximum value for each ID. I've tried a few other examples but nothing seems to work. Original data structure: ID state value (FLOAT) 1 TX …
Richard Todd
  • 2,406
  • 5
  • 32
  • 40
31
votes
2 answers

Get max value from row of a dataframe in python

This is my dataframe df a b c 1.2 2 0.1 2.1 1.1 3.2 0.2 1.9 8.8 3.3 7.8 0.12 I'm trying to get max value from each row of a dataframe, I m expecting output like this max_value 2 3.2 8.8 7.8 This is what I have…
Andre_k
  • 1,680
  • 3
  • 18
  • 41
31
votes
5 answers

which(vector1 < vector2)

Let's make a small example first, that computes in R: x<- c(1,3,1,4,2) max(which(x<2)) [1] 3 Now, I would like to do this not just for one value 2, but for many values simultaneously. It should give me something like…
MrHallo
  • 413
  • 3
  • 6
31
votes
1 answer

Find max value and show corresponding value from different field in SQL server

I have a table with data about cities that includes their name, population and other fields irrelevant to my question. ID Name Population 1 A 45667 2 B 123456 3 C 3005 4 D 13769 …
HDunn
  • 533
  • 2
  • 13
  • 26
30
votes
3 answers

Remove Max and Min values from python list of integers

I am not completely green to Python, but I am interested in learning/keeping good practices while I develop my skills. I want to remove the high and low values from a list of numbers, which I know how to do, but am curious if there is a…
Esteban
  • 2,444
  • 2
  • 19
  • 19
30
votes
5 answers

Is max(a,b) defined in stdlib.h or not?

I'm using two computers, each with a different version of visual studio. On the visual studio 2008 computer my code compiles. On the visual 2010 computer my code doesn't compile because I'm using the macro max(a,b) which as far as I know is defined…
snakile
  • 52,936
  • 62
  • 169
  • 241
30
votes
5 answers

MongooseJS - How to find the element with the maximum value?

I am using MongoDB , MongooseJS and Nodejs. I have a Collection ( called Member ) with the following Fields - Country_id , Member_id , Name, Score I want to write a query which returns the Member with the max Score where Country id = 10 I couldnt…
geeky_monster
  • 8,672
  • 18
  • 55
  • 86
30
votes
19 answers

Java: Finding the highest value in an array

For some reason this code is printing three values for the highest value in the array when I'm trying to print just one (which is 11.3). Can someone please explain to me why it is doing this? Thanks. import java.util.Scanner; public class…
HelloWorld
  • 559
  • 4
  • 11
  • 16
30
votes
8 answers

hive sql find the latest record

the table is: create table test ( id string, name string, age string, modified string) data like this: id name age modifed 1 a 10 2011-11-11 11:11:11 1 a 11 2012-11-11 12:00:00 2 b 20 2012-12-10 10:11:12 2 …
qiulp
  • 301
  • 1
  • 3
  • 4
30
votes
12 answers

Get most recent date from an array of dates in "Y-m-d H:i:s" format

I have the array of dates in Y-m-d H:i:s format like: array(5) { [0]=> string(19) "2012-06-11 08:30:49" [1]=> string(19) "2012-06-07 08:03:54" [2]=> string(19) "2012-05-26 23:04:04" [3]=> string(19) "2012-05-27 08:30:00" …
sugarFornaciari
  • 313
  • 1
  • 3
  • 7
29
votes
5 answers

Algorithm to find the maximum sum in a sequence of overlapping intervals

The problem I am trying to solve has a list of intervals on the number line, each with a pre-defined score. I need to return the maximum possible total score. The catch is that the intervals overlap, and of the overlapping intervals I can use only…
efficiencyIsBliss
  • 3,043
  • 7
  • 38
  • 44
29
votes
3 answers

What is the maximum length of an array in .NET on 64-bit Windows

I heard from someone that the maximum array size in .NET is 4 GB? Just wondering if that is true. You wouldn't dream of doing this on 32-bit .NET but on a 64-bit system with 12 GB of RAM, maybe, just maybe you might want to do this. :-)
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
28
votes
6 answers

How to efficiently retrieve the indices of maximum values in a Torch tensor?

Assume to have a torch tensor, for example of the following shape: x = torch.rand(20, 1, 120, 120) What I would like now, is to get the indices of the maximum values of each 120x120 matrix. To simplify the problem I would first x.squeeze() to work…
Chris
  • 1,266
  • 4
  • 16
  • 34