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
3 answers

R: Find set of columns which contain most 1s in matrix of 0 and 1

I have a matrix of 1s and 0s where the rows are individuals and the columns are events. A 1 indicates that an event happened to an individual and a 0 that it did not. I want to find which set of (in the example) 5 columns/events that cover the most…
embacify
  • 384
  • 1
  • 2
  • 9
2
votes
2 answers

For each row return the column names of the smallest value - pandas

Assuming that I have a dataframe with the following values: id product1sold product2sold product3sold 1 2 3 3 2 0 0 5 3 3 2 1 How do I add a…
Nele
  • 81
  • 1
  • 9
2
votes
1 answer

Max and Min is coming out as true instead of a number, what do I do?

I need the minimum and maximum number out of an array but it is telling me that the address of function will always come out to true instead of a number. I can't figure out what to change. #include using namespace std; int main() { …
2
votes
4 answers

MySQL Max Count without Order By

I have the following MySQL line: SELECT age, count(*) AS total FROM pacient WHERE age BETWEEN 20 AND 40 GROUP BY age ORDER BY age and I need to add an additional column to it that shows ONLY the max value of the count(*) for every row. For…
Luis Alvarado
  • 8,837
  • 12
  • 46
  • 60
2
votes
1 answer

SQL max function get other tables

I have a simple relation "sea" with only two columns, one is called "name" and the other "depth". With the following command, I can output the number maximum number in the attribute depth: SELECT max(depth) FROM sea; I am trying to get the the name…
Hemmelig
  • 803
  • 10
  • 22
2
votes
1 answer

How to replace values in a np 2d array based on condition for every row

I have a numpy 2d array (named lda_fit) with probabilities, where I want to replace the probabilities with 0 or 1, based on the max value in each line. array([[0.06478282, 0.80609092, 0.06511851, 0.06400775], [0.50386571, 0.02621445,…
Sebastian
  • 97
  • 1
  • 8
2
votes
1 answer

Python Pandas Dataframe idxmax is so slow. Alternatives?

I'm trying to select rows out of groups by max value using df.loc[df.groupby(keys)['column'].idxmax()]. I'm finding, however, that df.groupby(keys)['column'].idxmax() takes a really long time on my dataset of about 27M rows. Interestingly, running…
Keith A
  • 133
  • 1
  • 6
2
votes
1 answer

Postgres SQL Group By Until Next Row Different

I have following data in postgres: index | item | ------+-------- 10 | aaa | 20 | aaa | 30 | bbb | 40 | aaa | 50 | aaa | I would like to group it and get the minimum and maximum value. However, it only should group until…
obl0702
  • 123
  • 11
2
votes
2 answers

Perl Syntax combining `foreach` and `if`: Shouldn't it work, i.e.: Why doesn't it work?

Trying to get the maximum in an array that cannot contain negative numbers I tried: my @v; #... my $max = 0; $max = $_ if ($_ > $max) foreach (@v); I get a syntax error with perl 5.18.2. However (1) statement($_) foreach (@v); and (2)…
U. Windl
  • 3,480
  • 26
  • 54
2
votes
4 answers

How do I order a set of integers from smallest to greatest without if statements?

I'm currently stuck on a C++ problem that asks me to sort five inputted integers from least to greatest. I've tried using if statements to do this, but it just seems way too inefficient to list out all the possible combinations. I've also…
Anthony
  • 81
  • 7
2
votes
2 answers

Select max for a tuple in table

I have a table that looks like this: host, job, folder, file, mtime Folder names are not unique and can be same for a job spread across different hosts. I need to pick folder where the max(mtime for a file) is the max across all the folders by the…
Maxsteel
  • 1,922
  • 4
  • 30
  • 55
2
votes
2 answers

SQL MAX() omitting values

I have a table with a column "version" It specifies software version with a string "3.2.2.0" for example It can have too the "DEBUG" version I want to obtain the MAX value of the column, but omitting DEBUG, DEBUG only shown if is the only value…
Juan
  • 588
  • 1
  • 8
  • 25
2
votes
4 answers

Get max value of 3 columns from pandas DataFrame?

I've a Pandas DataFrame with 3 columns: c={'a': [['US']],'b': [['US']], 'c': [['US','BE']]} df = pd.DataFrame(c, columns = ['a','b','c']) Now I need the max value of these 3 columns. I've tried: df['max_val'] = df[['a','b','c']].max(axis=1) The…
John Doe
  • 9,843
  • 13
  • 42
  • 73
2
votes
2 answers

Get highest absolute value for each row (awk)

I have a file with multiple rows and columns tab delimeted such as: ID v1 v2 v3 v4 v5 v6 A10 -0.2134 -0.190 -0.114 0.400 10.678 -0.123 A115 -0.5038 -0.559 -0.664 0.431 0.139 -0.860 AAAS -0.9072 -0.990 …
Alex
  • 355
  • 1
  • 7
  • 20
2
votes
3 answers

MongoDB Find max salary of a department

{ id: '101', name: 'Ethan', department: 'IT', salary: 5000 }, { id: '102', name: 'Sally', department: 'Sales', salary: 6000 }, { id: '103', name: 'Harry', department: 'HR', salary: 4000 }, { id: '104', name: 'Jane', department: 'HR', salary: 3500…
insearchof
  • 49
  • 1