Returning All Maximum or Minimum Values That Can Be Multiple

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] and heapq.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



Leave a reply



Submit