Returning all maximum or minimum values that can be multiple
arr = [1, 2, 3, 5]
arr.group_by{|a| a % 3} # => {1=>[1], 2=>[2, 5], 0=>[3]}
arr.group_by{|a| a % 3}.max.last # => [2, 5]
What does Select Max return in VBA when there are multiple maximum values?
It will return the maximum value found in the column whether that value occurs once or more times.
MS Access Max SQL function
Edit: that is, it does not offer a count of the number of times that value occurs.
If you want the count of those occurrences of your peak value you would need to evaluate your recordset and count/identify the rows containing that value.
Something like SELECT ID, COLUMN2 FROM [invd] WHERE InvNum=(Select Max(invNum) from [invd])
may help illustrate that
How to force max to return ALL maximum values in a Java Stream?
I would group by value and store the values into a TreeMap
in order to have my values sorted, then I would get the max value by getting the last entry as next:
Stream.of(1, 3, 5, 3, 2, 3, 5)
.collect(groupingBy(Function.identity(), TreeMap::new, toList()))
.lastEntry()
.getValue()
.forEach(System.out::println);
Output:
5
5
Is there a more efficient way to grab multiple minimum values in an object in Javascript?
One loop, in comparison to looping by reduce and then through the object keys.
const obj = {
thingA: 1,
thingB: 2,
thingC: 1,
thingD: 1,
thingE: 4
}
function randomizeKey(_obj) {
let objKeysArr = [];
let minValue;
for (const property in _obj) {
if (minValue == undefined || _obj[property] < minValue) {
objKeysArr = [];
minValue = _obj[property];
}
if (_obj[property] == minValue) {
objKeysArr.push(property);
}
}
console.log(`all keys: ${objKeysArr}`);
return objKeysArr[randomize(objKeysArr.length)];
}
function randomize(max, min) {
return Math.floor(Math.random() * max) + (min || 0);
}
console.log( randomizeKey(obj) );
Which maximum does Python pick in the case of a tie?
It picks the first element it sees. See the documentation for max()
:
If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as
sorted(iterable, key=keyfunc, reverse=True)[0]
andheapq.nlargest(1, iterable, key=keyfunc)
.
In the source code this is implemented in ./Python/bltinmodule.c
by builtin_max
, which wraps the more general min_max
function.
min_max
will iterate through the values and use PyObject_RichCompareBool
to see if they are greater than the current value. If so, the greater value replaces it. Equal values will be skipped over.
The result is that the first maximum will be chosen in the case of a tie.
Related Topics
How to Use Gem to Install Rails on Ubuntu
Rails Sends 0 Byte Files Using Send_File
Mavericks, Rbenv, Your Ruby Version Is 2.0.0, But Your Gemfile Specified 2.1.1
Execute a Sudo Command in Ruby on Rails App
Rvm Warning! Path Is Not Properly Set Up
Fork Child Process with Timeout and Capture Output
Are Crlf Lines Ok in a Rails Project Deployed on Linux
Error Sudo: Gem: Command Not Found
Has Anyone Tried Installing Ruby & Rubygems from Source on Ubuntu (Preferably Ubuntu 9)
Can't Install Rails on MAC Os Catalina
Rvm 'Not Found' After Successful Usage and a Few Days Later
Limit Space and Memory Used by Imagemagick
Confirmation About Pgrep Returning Itself