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

Javascript find the closest number in an array of objects and retrieve the object's key value

I have an array of objects(with keys: name, quoteNumber)and I would like to find the closest quoteNumber that is smaller than a given number then retrieve that object's name, I have consider using a for loop to remove the larger values, and get the…
efgdh
  • 315
  • 5
  • 14
2
votes
1 answer

Java BigDecimal CompareTo Not Working with Small Numbers

I need to compare extremely small numbers for an artificial intelligence project. Here is my method: private static int maximum(BigDecimal a1, BigDecimal a2){ System.out.println(a1); System.out.println(a2); if (a1.compareTo(a2)<0){ …
2
votes
1 answer

How to select x value of max y in R

I have a table with an x and y value as follows:; cbm <- captialbikedata %>% group_by(hr) %>% summarize(users = sum(registered)) x y 1 23 2 45 in this case how would I select 2? (I want to select the x value with the highest y…
Christian
  • 35
  • 2
2
votes
2 answers

mysql get max value after min value

In mysql table, I want to get the max value that comes after the min value. SELECT * from `table` result: id date value -- ------------------- ----- 1 2021-03-03 13:14:05 1.15 2 2021-03-03 13:14:06 1.32 3 2021-03-03…
srtc
  • 33
  • 1
  • 4
2
votes
3 answers

Python Dataframe - Get max value between specific number vs. column value

When I have a below df, I want to get a column 'C' which has max value between specific value '15' and column 'A' within the condition "B == 't'" testdf = pd.DataFrame({"A":[20, 16, 7, 3, 8],"B":['t','t','t','t','f']}) testdf A B 0 20 t 1 …
corezal
  • 31
  • 2
2
votes
1 answer

How can I plot a smooth line over plot points, like a contour/skyline of the plot?

What I'm looking for is best explained by a picture: A line that "contours" the maxima of my points (like giving the "skyline" of the plot). I have a plot of scattered points with dense, (mostly) unique x coordinates (not equally distributed in…
Honeybear
  • 2,928
  • 2
  • 28
  • 47
2
votes
1 answer

Is there a way to combine across() and mutate() if I am referencing column names from a list?

The dataset below has columns with very similar names and some values which are NA. library(tidyverse) dat <- data.frame( v1_min = c(1,2,4,1,NA,4,2,2), v1_max = c(1,NA,5,4,5,4,6,NA), other_v1_min = c(1,1,NA,3,4,4,3,2), other_v1_max =…
Kim
  • 21
  • 1
2
votes
1 answer

Finding the max date between few date columns in each row - POWER BI

I assume my data looks like this (column names): name (String) age (Whole Number) date1 (Date) date2 (Date) date3 (Date) date4 (Date) gender (String) I would like to add a new column to the table that will contain the MAX date in each row between…
brian rik
  • 25
  • 1
  • 7
2
votes
1 answer

How to find min after a max value in DataFrame python?

Imagine for the following DataSet: data = {"row1":[1,2,3,4,5], "row2" : [2,3,4,1,3], "row3":[3,4,2,0,0] } data = pd.DataFrame(data) data = data.T print(data) 0 1 2 3 4 row1 1 2 3 4 5 row2 2 3 4 1 3 row3 3 4 2 0 0 I want…
Joe the Second
  • 339
  • 1
  • 11
2
votes
3 answers

Max of substring in column

Given that I have a column of values in the format 01.2020 12.2021 3.2019 02.2020 etc. over a million rows or so, I want a VBA function to find the maximum value of the digits to the left of the period. Something like Option Explicit Sub test() …
eirikdaude
  • 3,106
  • 6
  • 25
  • 50
2
votes
4 answers

Java: finding index of maximum from slice of an array

I have a large array. I have some Java code for identifying indices for start and end points for a subset/slice of that large array. The only information items that I need to retrieve from the selected subsection of the array are the indices and…
CodeMed
  • 9,527
  • 70
  • 212
  • 364
2
votes
6 answers

How to find the product with the maximum discount using SQL on HackerRank

I'm having trouble with an SQL problem on HackerRank (so no rank or window functions): Product_Id Product_Name Category Price Discount Available 1 P-1 C-5 720 10 1 2 P-2 …
Solana Liu
  • 45
  • 1
  • 1
  • 6
2
votes
5 answers

T-SQL: Selecting every instance of min and max values

I have a table called #TimeAtHome. It includes an address, the date and a flag atHome to indicate if the person was at home that day. I need to capture the min and max date for every grouping the person is not at home (0) for each address. Here is…
lumiukko
  • 249
  • 3
  • 13
2
votes
1 answer

Mutate IF-Else Statement using max value

I know this should be fairly easy to fix, but for some reason I'm not getting the correct output I'm searching for. I have a large dataset where I'm trying to create a new column based on two conditions if a count column is labeled as 1 and if that…
vizidea
  • 153
  • 7
2
votes
1 answer

simple program doesn't work with negative array numbers

I just started to learn C language. By now I have very basic knowledge. At the moment I am coding some very simple programs from given exercises by my University. Current exercise I have is: Write a program that finds two largest elements in n…
mezo101
  • 31
  • 4