On Tuesday, 30 May 2017 at 18:05:07 UTC, H. S. Teoh wrote:
On Tue, May 30, 2017 at 05:57:04PM +0000, Lyle via
Digitalmars-d-learn wrote:
Hi,
I have an associative array of type int[ulong] and I'm trying
to get the index of the maximum value, like this:
int[ulong] aa = [1UL: 2000,
2UL: 5000,
5UL: 1000];
writeln(aa.maxIndex); // should print 2
Can anyone help me out?
[...]
Try this:
void main() {
import std.algorithm.iteration : fold;
import std.array : byPair;
import std.stdio : writeln;
int[ulong] aa = [1UL: 2000,
2UL: 5000,
5UL: 1000];
writeln(aa.byPair
.fold!((a,b) => a[1] < b[1] ? b : a)[0]);
}
writeln(aa.byPair.maxElement!(p => p[1])[0]);
or with https://github.com/dlang/phobos/pull/5436 :
writeln(aa.byPair.maxElement!(p => p.value).key);