Questions tagged [min]

Minimum value. Smallest, tiniest, least.

Min() is a math library function available in many programming environments which returns the entity of smallest value from two or more candidate values.

Do not use for:

  • Question about minimal value of type (Long.MIN_VALUE, Integer.MIN_VALUE etc.)

Examples:

SQL

SQL MIN() function is one of aggregate functions. It returns the smallest value of column.
Example:

SELECT MIN(column_name) FROM table_name;

Python

In Python, the native min function identifies the smallest element of a set (e.g., a list):

>> min([1, 2, 3])
1

Java

In Java we can use java.lang.Math class to compute minimum value of two arguments. Example:

int minElement = Math.min(x, y);

To compute minimum element in collection we can use ,min() method from java.util.Collections class:

int minElement = Collections.min(list);

Ruby

In Ruby to compute minimum element of collection we simply use min function:

[4,7].min

Clojure

In Clojure we use min function in following way:

(min 1 2 3 4)

In above code `min' is function name and numbers 1..4 are arguments passed to it.
Or if we have list defined:

def myList [1 2 3 4]

then we can compute min this way:

(apply min myList)

R

In R we have function min.

x <- c(1, 2, 3)
y <- c(4, 5)
z <- c(0, NA)
min(x)
min(y)
min(z)
min(z, na.rm = TRUE)
min(x, y, z, na.rm = TRUE)  ## as same as min(c(x, y, z), na.rm = TRUE)
3013 questions
692
votes
25 answers

Getting the index of the returned max or min item using max()/min() on a list

I'm using Python's max and min functions on lists for a minimax algorithm, and I need the index of the value returned by max() or min(). In other words, I need to know which move produced the max (at a first player's turn) or min (second player)…
Kevin Griffin
  • 14,084
  • 7
  • 28
  • 23
451
votes
17 answers

Get the key corresponding to the minimum value within a dictionary

If I have a Python dictionary, how do I get the key to the entry which contains the minimum value? I was thinking about something to do with the min() function... Given the input: {320:1, 321:0, 322:3} It would return 321.
tjvr
  • 17,431
  • 6
  • 25
  • 26
407
votes
16 answers

MIN and MAX in C

Where are MIN and MAX defined in C, if at all? What is the best way to implement these, as generically and type safely as possible? (Compiler extensions/builtins for mainstream compilers preferred.)
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
212
votes
3 answers

How do I get the MIN() of two fields in Postgres?

Let's say I have a table like this: name | score_a | score_b -----+---------+-------- Joe | 100 | 24 Sam | 96 | 438 Bob | 76 | 101 ... | ... | ... I'd like to select the minimum of score_a and score_b. In other words,…
mike
  • 46,876
  • 44
  • 102
  • 112
209
votes
10 answers

How can I get the maximum or minimum value in a vector?

How can I get the maximum or minimum value in a vector in C++? And am I wrong in assuming it would be more or less the same with an array? I need an iterator, right? I tried it with max_element, but I kept getting an…
bob blob
  • 2,191
  • 3
  • 15
  • 6
160
votes
14 answers

Min/Max of dates in an array?

How can I find out the min and the max date from an array of dates? Currently, I am creating an array like this: var dates = []; dates.push(new Date("2011/06/25")) dates.push(new Date("2011/06/26")) dates.push(new Date("2011/06/27")) dates.push(new…
Legend
  • 113,822
  • 119
  • 272
  • 400
145
votes
5 answers

How to find the min/max value of a common key in a list of dicts?

I have a list of dictionaries like so: [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}] I want to find the min() and max() prices. Now, I can sort this easily enough using a key with a lambda expression (as found in…
Hank Fay
  • 1,704
  • 2
  • 11
  • 11
119
votes
19 answers

Obtain smallest value from array in Javascript?

Array justPrices has values such as: [0] = 1.5 [1] = 4.5 [2] = 9.9. How do I return the smallest value in the array?
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
115
votes
21 answers

What's the best way to select the minimum value from several columns?

Given the following table in SQL Server 2005: ID Col1 Col2 Col3 -- ---- ---- ---- 1 3 34 76 2 32 976 24 3 7 235 3 4 245 1 792 What is the best way to write the query that yields the…
stucampbell
  • 6,383
  • 5
  • 26
  • 25
98
votes
6 answers

How to achieve "MinOrDefault" in LINQ?

I'm producing a list of decimal values from a LINQ expression and I want the minimum non zero value. However it's entirely possible that the LINQ expression will result in an empty list. This will raise an exception and there is no MinOrDefault to…
Chris Simpson
  • 7,821
  • 10
  • 48
  • 68
98
votes
10 answers

Group by minimum value in one field while selecting distinct rows

Here's what I'm trying to do. Let's say I have this table t: key_id | id | record_date | other_cols 1 | 18 | 2011-04-03 | x 2 | 18 | 2012-05-19 | y 3 | 18 | 2012-08-09 | z 4 | 19 | 2009-06-01 | a 5 | 19 | 2011-04-03 |…
user2765924
  • 983
  • 1
  • 6
  • 4
87
votes
14 answers

Use of min and max functions in C++

From C++, are std::min and std::max preferable over fmin and fmax? For comparing two integers, do they provide basically the same functionality? Do you tend to use one of these sets of functions or do you prefer to write your own (perhaps to…
bporter
  • 3,572
  • 4
  • 24
  • 23
81
votes
4 answers

Tuple pairs, finding minimum using python

I want to find the minimum of a list of tuples sorting by a given column. I have some data arranged as a list of 2-tuples for example. data = [ (1, 7.57), (2, 2.1), (3, 1.2), (4, 2.1), (5, 0.01), (6, 0.5), (7, 0.2), (8, 0.6)] How may I…
Harry Lime
  • 2,167
  • 8
  • 31
  • 53
75
votes
3 answers

Using std::max_element on a vector

I'm trying to use std::min_element and std::max_element to return the minimum and maximum elements in a vector of doubles. My compiler doesn't like how I'm currently trying to use them, and I don't understand the error message. I could of course…
synaptik
  • 8,971
  • 16
  • 71
  • 98
63
votes
5 answers

How to limit a number to be within a specified range? (Python)

I want to limit a number to be within a certain range. Currently, I am doing the following: minN = 1 maxN = 10 n = something() #some return value from a function n = max(minN, n) n = min(maxN, n) This keeps it within minN and maxN, but it doesn't…
Mantis Toboggan
  • 7,295
  • 3
  • 19
  • 11
1
2 3
99 100